Home | History | Annotate | Download | only in soong
      1 //
      2 // Copyright (C) 2017 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 package clangprebuilts
     18 
     19 import (
     20 	"path"
     21 	"strings"
     22 
     23 	"github.com/google/blueprint/proptools"
     24 
     25 	"android/soong/android"
     26 	"android/soong/cc"
     27 	"android/soong/cc/config"
     28 )
     29 
     30 // This module is used to generate libfuzzer, libomp static libraries and
     31 // libclang_rt.* shared libraries. When LLVM_PREBUILTS_VERSION and
     32 // LLVM_RELEASE_VERSION are set, the library will generated from the given
     33 // path.
     34 
     35 func init() {
     36 	android.RegisterModuleType("llvm_prebuilt_library_static",
     37 		llvmPrebuiltLibraryStaticFactory)
     38 	android.RegisterModuleType("libclang_rt_prebuilt_library_shared",
     39 		libClangRtPrebuiltLibrarySharedFactory)
     40 	android.RegisterModuleType("libclang_rt_prebuilt_library_static",
     41 		libClangRtPrebuiltLibraryStaticFactory)
     42 	android.RegisterModuleType("libclang_rt_llndk_library",
     43 		libClangRtLLndkLibraryFactory)
     44 }
     45 
     46 func getClangPrebuiltDir(ctx android.LoadHookContext) string {
     47 	return path.Join(
     48 		"./",
     49 		ctx.AConfig().GetenvWithDefault("LLVM_PREBUILTS_VERSION", config.ClangDefaultVersion),
     50 	)
     51 }
     52 
     53 func getClangResourceDir(ctx android.LoadHookContext) string {
     54 	clangDir := getClangPrebuiltDir(ctx)
     55 	releaseVersion := ctx.AConfig().GetenvWithDefault("LLVM_RELEASE_VERSION",
     56 		config.ClangDefaultShortVersion)
     57 	return path.Join(clangDir, "lib64", "clang", releaseVersion, "lib", "linux")
     58 }
     59 
     60 type archProps struct {
     61 	Android_arm struct {
     62 		Srcs []string
     63 	}
     64 	Android_arm64 struct {
     65 		Srcs []string
     66 	}
     67 	Android_mips struct {
     68 		Srcs []string
     69 	}
     70 	Android_mips64 struct {
     71 		Srcs []string
     72 	}
     73 	Android_x86 struct {
     74 		Srcs []string
     75 	}
     76 	Android_x86_64 struct {
     77 		Srcs []string
     78 	}
     79 }
     80 
     81 func llvmPrebuiltLibraryStatic(ctx android.LoadHookContext) {
     82 	libDir := getClangResourceDir(ctx)
     83 	name := strings.TrimPrefix(ctx.ModuleName(), "prebuilt_") + ".a"
     84 
     85 	type props struct {
     86 		Export_include_dirs []string
     87 		Target              archProps
     88 	}
     89 
     90 	p := &props{}
     91 
     92 	if (name == "libFuzzer.a") {
     93 		headerDir := path.Join(getClangPrebuiltDir(ctx), "prebuilt_include", "llvm", "lib", "Fuzzer")
     94 		p.Export_include_dirs = []string{headerDir}
     95 	}
     96 
     97 	p.Target.Android_arm.Srcs = []string{path.Join(libDir, "arm", name)}
     98 	p.Target.Android_arm64.Srcs = []string{path.Join(libDir, "aarch64", name)}
     99 	p.Target.Android_mips.Srcs = []string{path.Join(libDir, "mipsel", name)}
    100 	p.Target.Android_mips64.Srcs = []string{path.Join(libDir, "mips64el", name)}
    101 	p.Target.Android_x86.Srcs = []string{path.Join(libDir, "i386", name)}
    102 	p.Target.Android_x86_64.Srcs = []string{path.Join(libDir, "x86_64", name)}
    103 	ctx.AppendProperties(p)
    104 }
    105 
    106 func libClangRtPrebuiltLibraryShared(ctx android.LoadHookContext) {
    107 	if ctx.AConfig().IsEnvTrue("FORCE_BUILD_SANITIZER_SHARED_OBJECTS") {
    108 		return
    109 	}
    110 
    111 	libDir := getClangResourceDir(ctx)
    112 
    113 	type props struct {
    114 		Srcs               []string
    115 		System_shared_libs []string
    116 		Sanitize           struct {
    117 			Never *bool
    118 		}
    119 		Strip struct {
    120 			None *bool
    121 		}
    122 		Pack_relocations *bool
    123 		Stl              *string
    124 	}
    125 
    126 	p := &props{}
    127 
    128 	name := strings.TrimPrefix(ctx.ModuleName(), "prebuilt_")
    129 
    130 	p.Srcs = []string{path.Join(libDir, name+".so")}
    131 	p.System_shared_libs = []string{}
    132 	p.Sanitize.Never = proptools.BoolPtr(true)
    133 	p.Strip.None = proptools.BoolPtr(true)
    134 	disable := false
    135 	p.Pack_relocations = &disable
    136 	none := "none"
    137 	p.Stl = &none
    138 	ctx.AppendProperties(p)
    139 }
    140 
    141 func libClangRtPrebuiltLibraryStatic(ctx android.LoadHookContext) {
    142 	libDir := getClangResourceDir(ctx)
    143 
    144 	type props struct {
    145 		Srcs []string
    146 	}
    147 
    148 	name := strings.TrimPrefix(ctx.ModuleName(), "prebuilt_")
    149 
    150 	p := &props{}
    151 	p.Srcs = []string{path.Join(libDir, name+".a")}
    152 	ctx.AppendProperties(p)
    153 }
    154 
    155 func libClangRtLLndkLibrary(ctx android.LoadHookContext) {
    156 	libDir := getClangResourceDir(ctx)
    157 
    158 	type props struct {
    159 		Symbol_file *string
    160 	}
    161 
    162 	p := &props{}
    163 	symbol_file := string(path.Join(libDir, strings.TrimSuffix(ctx.ModuleName(), ".llndk") + ".map.txt"))
    164 	p.Symbol_file = proptools.StringPtr(symbol_file)
    165 	ctx.AppendProperties(p)
    166 }
    167 
    168 func llvmPrebuiltLibraryStaticFactory() android.Module {
    169 	module, _ := cc.NewPrebuiltStaticLibrary(android.DeviceSupported)
    170 	android.AddLoadHook(module, llvmPrebuiltLibraryStatic)
    171 	return module.Init()
    172 }
    173 
    174 func libClangRtPrebuiltLibrarySharedFactory() android.Module {
    175 	module, _ := cc.NewPrebuiltSharedLibrary(android.DeviceSupported)
    176 	android.AddLoadHook(module, libClangRtPrebuiltLibraryShared)
    177 	return module.Init()
    178 }
    179 
    180 func libClangRtPrebuiltLibraryStaticFactory() android.Module {
    181 	module, _ := cc.NewPrebuiltStaticLibrary(android.DeviceSupported)
    182 	android.AddLoadHook(module, libClangRtPrebuiltLibraryStatic)
    183 	return module.Init()
    184 }
    185 
    186 func libClangRtLLndkLibraryFactory() android.Module {
    187 	module := cc.NewLLndkStubLibrary()
    188 	android.AddLoadHook(module, libClangRtLLndkLibrary)
    189 	return module.Init()
    190 }
    191