github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/cc/prebuilt.go (about)

     1  // Copyright 2016 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cc
    16  
    17  import (
    18  	"android/soong/android"
    19  )
    20  
    21  func init() {
    22  	android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory)
    23  	android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory)
    24  	android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
    25  }
    26  
    27  type prebuiltLinkerInterface interface {
    28  	Name(string) string
    29  	prebuilt() *android.Prebuilt
    30  }
    31  
    32  type prebuiltLinker struct {
    33  	android.Prebuilt
    34  	properties struct {
    35  		Srcs []string `android:"arch_variant"`
    36  	}
    37  }
    38  
    39  func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
    40  	return &p.Prebuilt
    41  }
    42  
    43  func (p *prebuiltLinker) PrebuiltSrcs() []string {
    44  	return p.properties.Srcs
    45  }
    46  
    47  type prebuiltLibraryLinker struct {
    48  	*libraryDecorator
    49  	prebuiltLinker
    50  }
    51  
    52  var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
    53  
    54  func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
    55  	flags Flags, deps PathDeps, objs Objects) android.Path {
    56  	// TODO(ccross): verify shared library dependencies
    57  	if len(p.properties.Srcs) > 0 {
    58  		p.libraryDecorator.exportIncludes(ctx, "-I")
    59  		p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
    60  		p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
    61  		// TODO(ccross): .toc optimization, stripping, packing
    62  		return p.Prebuilt.SingleSourcePath(ctx)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func prebuiltSharedLibraryFactory() android.Module {
    69  	module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
    70  	return module.Init()
    71  }
    72  
    73  func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
    74  	module, library := NewLibrary(hod)
    75  	library.BuildOnlyShared()
    76  	module.compiler = nil
    77  
    78  	prebuilt := &prebuiltLibraryLinker{
    79  		libraryDecorator: library,
    80  	}
    81  	module.linker = prebuilt
    82  
    83  	module.AddProperties(&prebuilt.properties)
    84  
    85  	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
    86  	return module, library
    87  }
    88  
    89  func prebuiltStaticLibraryFactory() android.Module {
    90  	module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
    91  	return module.Init()
    92  }
    93  
    94  func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
    95  	module, library := NewLibrary(hod)
    96  	library.BuildOnlyStatic()
    97  	module.compiler = nil
    98  
    99  	prebuilt := &prebuiltLibraryLinker{
   100  		libraryDecorator: library,
   101  	}
   102  	module.linker = prebuilt
   103  
   104  	module.AddProperties(&prebuilt.properties)
   105  
   106  	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
   107  	return module, library
   108  }
   109  
   110  type prebuiltBinaryLinker struct {
   111  	*binaryDecorator
   112  	prebuiltLinker
   113  }
   114  
   115  var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
   116  
   117  func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
   118  	flags Flags, deps PathDeps, objs Objects) android.Path {
   119  	// TODO(ccross): verify shared library dependencies
   120  	if len(p.properties.Srcs) > 0 {
   121  		// TODO(ccross): .toc optimization, stripping, packing
   122  
   123  		// Copy binaries to a name matching the final installed name
   124  		fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
   125  		outputFile := android.PathForModuleOut(ctx, fileName)
   126  
   127  		ctx.Build(pctx, android.BuildParams{
   128  			Rule:        android.CpExecutable,
   129  			Description: "prebuilt",
   130  			Output:      outputFile,
   131  			Input:       p.Prebuilt.SingleSourcePath(ctx),
   132  		})
   133  
   134  		return outputFile
   135  	}
   136  
   137  	return nil
   138  }
   139  
   140  func prebuiltBinaryFactory() android.Module {
   141  	module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
   142  	return module.Init()
   143  }
   144  
   145  func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
   146  	module, binary := NewBinary(hod)
   147  	module.compiler = nil
   148  
   149  	prebuilt := &prebuiltBinaryLinker{
   150  		binaryDecorator: binary,
   151  	}
   152  	module.linker = prebuilt
   153  
   154  	module.AddProperties(&prebuilt.properties)
   155  
   156  	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
   157  	return module, binary
   158  }