Home | History | Annotate | Download | only in cc
      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 	"fmt"
     19 
     20 	"android/soong/android"
     21 )
     22 
     23 //
     24 // Objects (for crt*.o)
     25 //
     26 
     27 func init() {
     28 	android.RegisterModuleType("cc_object", objectFactory)
     29 }
     30 
     31 type objectLinker struct {
     32 	*baseLinker
     33 	Properties ObjectLinkerProperties
     34 }
     35 
     36 func objectFactory() android.Module {
     37 	module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
     38 	module.linker = &objectLinker{
     39 		baseLinker: NewBaseLinker(),
     40 	}
     41 	module.compiler = NewBaseCompiler()
     42 	return module.Init()
     43 }
     44 
     45 func (object *objectLinker) appendLdflags(flags []string) {
     46 	panic(fmt.Errorf("appendLdflags on object Linker not supported"))
     47 }
     48 
     49 func (object *objectLinker) linkerProps() []interface{} {
     50 	return []interface{}{&object.Properties}
     51 }
     52 
     53 func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
     54 
     55 func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
     56 	deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
     57 	return deps
     58 }
     59 
     60 func (*objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
     61 	if flags.Clang {
     62 		flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
     63 	} else {
     64 		flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
     65 	}
     66 
     67 	return flags
     68 }
     69 
     70 func (object *objectLinker) link(ctx ModuleContext,
     71 	flags Flags, deps PathDeps, objs Objects) android.Path {
     72 
     73 	objs = objs.Append(deps.Objs)
     74 
     75 	var outputFile android.Path
     76 	if len(objs.objFiles) == 1 {
     77 		outputFile = objs.objFiles[0]
     78 	} else {
     79 		output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
     80 		TransformObjsToObj(ctx, objs.objFiles, flagsToBuilderFlags(flags), output)
     81 		outputFile = output
     82 	}
     83 
     84 	ctx.CheckbuildFile(outputFile)
     85 	return outputFile
     86 }
     87