compiler.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright 2019 The Android Open Source Project
  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. package rust
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/rust/config"
  22. )
  23. type RustLinkage int
  24. const (
  25. DefaultLinkage RustLinkage = iota
  26. RlibLinkage
  27. DylibLinkage
  28. )
  29. func (compiler *baseCompiler) edition() string {
  30. return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
  31. }
  32. func (compiler *baseCompiler) setNoStdlibs() {
  33. compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
  34. }
  35. func (compiler *baseCompiler) disableLints() {
  36. compiler.Properties.Lints = proptools.StringPtr("none")
  37. }
  38. func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
  39. return &baseCompiler{
  40. Properties: BaseCompilerProperties{},
  41. dir: dir,
  42. dir64: dir64,
  43. location: location,
  44. }
  45. }
  46. type installLocation int
  47. const (
  48. InstallInSystem installLocation = 0
  49. InstallInData = iota
  50. incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
  51. genSubDir = "out/"
  52. )
  53. type BaseCompilerProperties struct {
  54. // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
  55. // Only a single source file can be defined. Modules which generate source can be included by prefixing
  56. // the module name with ":", for example ":libfoo_bindgen"
  57. //
  58. // If no source file is defined, a single generated source module can be defined to be used as the main source.
  59. Srcs []string `android:"path,arch_variant"`
  60. // name of the lint set that should be used to validate this module.
  61. //
  62. // Possible values are "default" (for using a sensible set of lints
  63. // depending on the module's location), "android" (for the strictest
  64. // lint set that applies to all Android platform code), "vendor" (for
  65. // a relaxed set) and "none" (for ignoring all lint warnings and
  66. // errors). The default value is "default".
  67. Lints *string
  68. // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
  69. Flags []string `android:"arch_variant"`
  70. // flags to pass to the linker
  71. Ld_flags []string `android:"arch_variant"`
  72. // list of rust rlib crate dependencies
  73. Rlibs []string `android:"arch_variant"`
  74. // list of rust dylib crate dependencies
  75. Dylibs []string `android:"arch_variant"`
  76. // list of rust automatic crate dependencies
  77. Rustlibs []string `android:"arch_variant"`
  78. // list of rust proc_macro crate dependencies
  79. Proc_macros []string `android:"arch_variant"`
  80. // list of C shared library dependencies
  81. Shared_libs []string `android:"arch_variant"`
  82. // list of C static library dependencies. These dependencies do not normally propagate to dependents
  83. // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
  84. Static_libs []string `android:"arch_variant"`
  85. // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
  86. // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
  87. // result in bloat if multiple dependencies all include the same static library whole.
  88. //
  89. // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
  90. // having to redeclare the static library dependency for every dependent module.
  91. // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
  92. // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
  93. //
  94. // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
  95. //
  96. // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
  97. // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
  98. Whole_static_libs []string `android:"arch_variant"`
  99. // list of Rust system library dependencies.
  100. //
  101. // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
  102. // like `core` and `alloc`.
  103. Stdlibs []string `android:"arch_variant"`
  104. // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
  105. // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
  106. // source, and is required to conform to an enforced format matching library output files (if the output file is
  107. // lib<someName><suffix>, the crate_name property must be <someName>).
  108. Crate_name string `android:"arch_variant"`
  109. // list of features to enable for this crate
  110. Features []string `android:"arch_variant"`
  111. // list of configuration options to enable for this crate. To enable features, use the "features" property.
  112. Cfgs []string `android:"arch_variant"`
  113. // specific rust edition that should be used if the default version is not desired
  114. Edition *string `android:"arch_variant"`
  115. // sets name of the output
  116. Stem *string `android:"arch_variant"`
  117. // append to name of output
  118. Suffix *string `android:"arch_variant"`
  119. // install to a subdirectory of the default install path for the module
  120. Relative_install_path *string `android:"arch_variant"`
  121. // whether to suppress inclusion of standard crates - defaults to false
  122. No_stdlibs *bool
  123. // Change the rustlibs linkage to select rlib linkage by default for device targets.
  124. // Also link libstd as an rlib as well on device targets.
  125. // Note: This is the default behavior for host targets.
  126. //
  127. // This is primarily meant for rust_binary and rust_ffi modules where the default
  128. // linkage of libstd might need to be overridden in some use cases. This should
  129. // generally be avoided with other module types since it may cause collisions at
  130. // linkage if all dependencies of the root binary module do not link against libstd\
  131. // the same way.
  132. Prefer_rlib *bool `android:"arch_variant"`
  133. // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
  134. // Will set CARGO_CRATE_NAME to the crate_name property's value.
  135. // Will set CARGO_BIN_NAME to the output filename value without the extension.
  136. Cargo_env_compat *bool
  137. // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
  138. Cargo_pkg_version *string
  139. }
  140. type baseCompiler struct {
  141. Properties BaseCompilerProperties
  142. // Install related
  143. dir string
  144. dir64 string
  145. subDir string
  146. relative string
  147. path android.InstallPath
  148. location installLocation
  149. sanitize *sanitize
  150. distFile android.OptionalPath
  151. // unstripped output file.
  152. unstrippedOutputFile android.Path
  153. // stripped output file.
  154. strippedOutputFile android.OptionalPath
  155. // If a crate has a source-generated dependency, a copy of the source file
  156. // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
  157. cargoOutDir android.ModuleOutPath
  158. }
  159. func (compiler *baseCompiler) Disabled() bool {
  160. return false
  161. }
  162. func (compiler *baseCompiler) SetDisabled() {
  163. panic("baseCompiler does not implement SetDisabled()")
  164. }
  165. func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
  166. panic("baseCompiler does not implement coverageOutputZipPath()")
  167. }
  168. func (compiler *baseCompiler) preferRlib() bool {
  169. return Bool(compiler.Properties.Prefer_rlib)
  170. }
  171. func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
  172. // For devices, we always link stdlibs in as dylibs by default.
  173. if compiler.preferRlib() {
  174. return RlibLinkage
  175. } else if ctx.Device() {
  176. return DylibLinkage
  177. } else {
  178. return RlibLinkage
  179. }
  180. }
  181. var _ compiler = (*baseCompiler)(nil)
  182. func (compiler *baseCompiler) inData() bool {
  183. return compiler.location == InstallInData
  184. }
  185. func (compiler *baseCompiler) compilerProps() []interface{} {
  186. return []interface{}{&compiler.Properties}
  187. }
  188. func (compiler *baseCompiler) cfgsToFlags() []string {
  189. flags := []string{}
  190. for _, cfg := range compiler.Properties.Cfgs {
  191. flags = append(flags, "--cfg '"+cfg+"'")
  192. }
  193. return flags
  194. }
  195. func (compiler *baseCompiler) featuresToFlags() []string {
  196. flags := []string{}
  197. for _, feature := range compiler.Properties.Features {
  198. flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
  199. }
  200. return flags
  201. }
  202. func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
  203. flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
  204. flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
  205. return flags
  206. }
  207. func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
  208. if ctx.RustModule().UseVndk() {
  209. compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
  210. }
  211. flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
  212. flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
  213. return flags
  214. }
  215. func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  216. lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
  217. if err != nil {
  218. ctx.PropertyErrorf("lints", err.Error())
  219. }
  220. // linkage-related flags are disallowed.
  221. for _, s := range compiler.Properties.Ld_flags {
  222. if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
  223. ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
  224. }
  225. }
  226. for _, s := range compiler.Properties.Flags {
  227. if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
  228. ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
  229. }
  230. if strings.HasPrefix(s, "--extern") {
  231. ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
  232. }
  233. if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
  234. ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
  235. }
  236. }
  237. flags.RustFlags = append(flags.RustFlags, lintFlags)
  238. flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
  239. flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
  240. flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
  241. flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
  242. flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
  243. flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
  244. flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
  245. flags.EmitXrefs = ctx.Config().EmitXrefRules()
  246. if ctx.Host() && !ctx.Windows() {
  247. rpathPrefix := `\$$ORIGIN/`
  248. if ctx.Darwin() {
  249. rpathPrefix = "@loader_path/"
  250. }
  251. var rpath string
  252. if ctx.toolchain().Is64Bit() {
  253. rpath = "lib64"
  254. } else {
  255. rpath = "lib"
  256. }
  257. flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+rpath)
  258. flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+"../"+rpath)
  259. }
  260. return flags
  261. }
  262. func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  263. panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
  264. }
  265. func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
  266. deps PathDeps) android.OptionalPath {
  267. return android.OptionalPath{}
  268. }
  269. func (compiler *baseCompiler) initialize(ctx ModuleContext) {
  270. compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
  271. }
  272. func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
  273. return android.OptionalPathForPath(compiler.cargoOutDir)
  274. }
  275. func (compiler *baseCompiler) CargoEnvCompat() bool {
  276. return Bool(compiler.Properties.Cargo_env_compat)
  277. }
  278. func (compiler *baseCompiler) CargoPkgVersion() string {
  279. return String(compiler.Properties.Cargo_pkg_version)
  280. }
  281. func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
  282. return compiler.unstrippedOutputFile
  283. }
  284. func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
  285. return compiler.strippedOutputFile
  286. }
  287. func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
  288. deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
  289. deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
  290. deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
  291. deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
  292. deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
  293. deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
  294. deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
  295. deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
  296. if !Bool(compiler.Properties.No_stdlibs) {
  297. for _, stdlib := range config.Stdlibs {
  298. // If we're building for the build host, use the prebuilt stdlibs
  299. if ctx.Target().Os == android.Linux || ctx.Target().Os == android.Darwin {
  300. stdlib = "prebuilt_" + stdlib
  301. }
  302. deps.Stdlibs = append(deps.Stdlibs, stdlib)
  303. }
  304. }
  305. return deps
  306. }
  307. func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
  308. bionicLibs := []string{}
  309. bionicLibs = append(bionicLibs, "liblog")
  310. bionicLibs = append(bionicLibs, "libc")
  311. bionicLibs = append(bionicLibs, "libm")
  312. bionicLibs = append(bionicLibs, "libdl")
  313. if static {
  314. deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
  315. } else {
  316. deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
  317. }
  318. if ctx.RustModule().StaticExecutable() {
  319. deps.StaticLibs = append(deps.StaticLibs, "libunwind")
  320. }
  321. if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
  322. deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
  323. }
  324. return deps
  325. }
  326. func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
  327. muslLibs := []string{"libc_musl"}
  328. if static {
  329. deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
  330. } else {
  331. deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
  332. }
  333. if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
  334. deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
  335. }
  336. return deps
  337. }
  338. func (compiler *baseCompiler) crateName() string {
  339. return compiler.Properties.Crate_name
  340. }
  341. func (compiler *baseCompiler) everInstallable() bool {
  342. // Most modules are installable, so return true by default.
  343. return true
  344. }
  345. func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
  346. dir := compiler.dir
  347. if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
  348. dir = compiler.dir64
  349. }
  350. if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
  351. dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
  352. }
  353. if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
  354. dir = filepath.Join(dir, ctx.Arch().ArchType.String())
  355. }
  356. if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
  357. if ctx.RustModule().InProduct() {
  358. dir = filepath.Join(dir, "product")
  359. } else if ctx.RustModule().InVendor() {
  360. dir = filepath.Join(dir, "vendor")
  361. } else {
  362. ctx.ModuleErrorf("Unknown data+VNDK installation kind")
  363. }
  364. }
  365. return android.PathForModuleInstall(ctx, dir, compiler.subDir,
  366. compiler.relativeInstallPath(), compiler.relative)
  367. }
  368. func (compiler *baseCompiler) nativeCoverage() bool {
  369. return false
  370. }
  371. func (compiler *baseCompiler) install(ctx ModuleContext) {
  372. path := ctx.RustModule().OutputFile()
  373. compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
  374. }
  375. func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
  376. return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
  377. }
  378. func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
  379. stem := ctx.ModuleName()
  380. if String(compiler.Properties.Stem) != "" {
  381. stem = String(compiler.Properties.Stem)
  382. }
  383. return stem
  384. }
  385. func (compiler *baseCompiler) relativeInstallPath() string {
  386. return String(compiler.Properties.Relative_install_path)
  387. }
  388. // Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
  389. func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
  390. if len(srcs) == 0 {
  391. ctx.PropertyErrorf("srcs", "srcs must not be empty")
  392. }
  393. // The srcs can contain strings with prefix ":".
  394. // They are dependent modules of this module, with android.SourceDepTag.
  395. // They are not the main source file compiled by rustc.
  396. numSrcs := 0
  397. srcIndex := 0
  398. for i, s := range srcs {
  399. if android.SrcIsModule(s) == "" {
  400. numSrcs++
  401. srcIndex = i
  402. }
  403. }
  404. if numSrcs > 1 {
  405. ctx.PropertyErrorf("srcs", incorrectSourcesError)
  406. }
  407. // If a main source file is not provided we expect only a single SourceProvider module to be defined
  408. // within srcs, with the expectation that the first source it provides is the entry point.
  409. if srcIndex != 0 {
  410. ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
  411. } else if numSrcs > 1 {
  412. ctx.PropertyErrorf("srcs", "only a single generated source module can be defined without a main source file.")
  413. }
  414. paths := android.PathsForModuleSrc(ctx, srcs)
  415. return paths[srcIndex], paths[1:]
  416. }