| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 1 | // Copyright 2020 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 |  | 
 | 15 | package rust | 
 | 16 |  | 
 | 17 | import ( | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 18 | 	"strings" | 
 | 19 |  | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 20 | 	"github.com/google/blueprint" | 
| Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 21 | 	"github.com/google/blueprint/proptools" | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 22 |  | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 23 | 	"android/soong/android" | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 24 | 	"android/soong/cc" | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 25 | 	cc_config "android/soong/cc/config" | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 26 | ) | 
 | 27 |  | 
 | 28 | var ( | 
| Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 29 | 	defaultBindgenFlags = []string{""} | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 30 |  | 
 | 31 | 	// bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures. | 
| Yi Kong | 06521c4 | 2023-09-07 07:18:50 +0000 | [diff] [blame] | 32 | 	bindgenClangVersion = "clang-r498229b" | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 33 |  | 
| Peter Collingbourne | 9a868f1 | 2020-12-30 20:23:01 -0800 | [diff] [blame] | 34 | 	_ = pctx.VariableFunc("bindgenClangVersion", func(ctx android.PackageVarContext) string { | 
 | 35 | 		if override := ctx.Config().Getenv("LLVM_BINDGEN_PREBUILTS_VERSION"); override != "" { | 
 | 36 | 			return override | 
 | 37 | 		} | 
 | 38 | 		return bindgenClangVersion | 
 | 39 | 	}) | 
 | 40 |  | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 41 | 	//TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen. | 
| Shao-Chuan Lee | aa3231c | 2020-11-05 22:20:17 +0900 | [diff] [blame] | 42 | 	_ = pctx.HostBinToolVariable("bindgenCmd", "bindgen") | 
| Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame] | 43 | 	_ = pctx.VariableFunc("bindgenHostPrebuiltTag", func(ctx android.PackageVarContext) string { | 
 | 44 | 		if ctx.Config().UseHostMusl() { | 
 | 45 | 			// This is a hack to use the glibc bindgen binary until we have a musl version checked in. | 
 | 46 | 			return "linux-x86" | 
 | 47 | 		} else { | 
 | 48 | 			return "${config.HostPrebuiltTag}" | 
 | 49 | 		} | 
 | 50 | 	}) | 
 | 51 | 	_ = pctx.VariableFunc("bindgenClangLibdir", func(ctx android.PackageVarContext) string { | 
 | 52 | 		if ctx.Config().UseHostMusl() { | 
| Colin Cross | 645874d | 2022-10-14 14:18:21 -0700 | [diff] [blame] | 53 | 			return "musl/lib/" | 
| Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame] | 54 | 		} else { | 
| Yi Kong | bd18881 | 2023-02-08 19:51:59 +0900 | [diff] [blame] | 55 | 			return "lib/" | 
| Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame] | 56 | 		} | 
 | 57 | 	}) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 58 | 	_ = pctx.SourcePathVariable("bindgenClang", | 
| Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame] | 59 | 		"${cc_config.ClangBase}/${bindgenHostPrebuiltTag}/${bindgenClangVersion}/bin/clang") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 60 | 	_ = pctx.SourcePathVariable("bindgenLibClang", | 
| Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame] | 61 | 		"${cc_config.ClangBase}/${bindgenHostPrebuiltTag}/${bindgenClangVersion}/${bindgenClangLibdir}") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 62 |  | 
 | 63 | 	//TODO(ivanlozano) Switch this to RuleBuilder | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 64 | 	// | 
 | 65 | 	//TODO Pass the flag files directly to bindgen e.g. with @file when it supports that. | 
 | 66 | 	//See https://github.com/rust-lang/rust-bindgen/issues/2508. | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 67 | 	bindgen = pctx.AndroidStaticRule("bindgen", | 
 | 68 | 		blueprint.RuleParams{ | 
| Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 69 | 			Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " + | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 70 | 				"$cmd $flags $$(cat $flagfiles) $in -o $out -- -MD -MF $out.d $cflags", | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 71 | 			CommandDeps: []string{"$cmd"}, | 
| Ivan Lozano | e1e844b | 2020-07-24 15:16:30 -0400 | [diff] [blame] | 72 | 			Deps:        blueprint.DepsGCC, | 
 | 73 | 			Depfile:     "$out.d", | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 74 | 		}, | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 75 | 		"cmd", "flags", "flagfiles", "cflags") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 76 | ) | 
 | 77 |  | 
 | 78 | func init() { | 
 | 79 | 	android.RegisterModuleType("rust_bindgen", RustBindgenFactory) | 
| Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 80 | 	android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 81 | } | 
 | 82 |  | 
 | 83 | var _ SourceProvider = (*bindgenDecorator)(nil) | 
 | 84 |  | 
 | 85 | type BindgenProperties struct { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 86 | 	// The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp". | 
 | 87 | 	// This is used to specify how to interpret the header and determines which '-std' flag to use by default. | 
 | 88 | 	// | 
 | 89 | 	// If your C++ header must have some other extension, then the default behavior can be overridden by setting the | 
 | 90 | 	// cpp_std property. | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 91 | 	Wrapper_src *string `android:"path,arch_variant"` | 
 | 92 |  | 
 | 93 | 	// list of bindgen-specific flags and options | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 94 | 	Bindgen_flags []string `android:"arch_variant"` | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 95 |  | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 96 | 	// list of files containing extra bindgen flags | 
 | 97 | 	Bindgen_flag_files []string `android:"arch_variant"` | 
 | 98 |  | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 99 | 	// module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom | 
 | 100 | 	// binary must expect arguments in a similar fashion to bindgen, e.g. | 
 | 101 | 	// | 
 | 102 | 	// "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]" | 
| Liz Kammer | eda9398 | 2021-04-20 10:15:41 -0400 | [diff] [blame] | 103 | 	Custom_bindgen string | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 104 | } | 
 | 105 |  | 
 | 106 | type bindgenDecorator struct { | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 107 | 	*BaseSourceProvider | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 108 |  | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 109 | 	Properties      BindgenProperties | 
 | 110 | 	ClangProperties cc.RustBindgenClangProperties | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 111 | } | 
 | 112 |  | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 113 | func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) { | 
 | 114 | 	// Assume headers are C headers | 
 | 115 | 	isCpp := false | 
 | 116 | 	stdVersion := "" | 
 | 117 |  | 
 | 118 | 	switch src.Ext() { | 
 | 119 | 	case ".hpp", ".hh": | 
 | 120 | 		isCpp = true | 
 | 121 | 	} | 
 | 122 |  | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 123 | 	if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 124 | 		ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.") | 
 | 125 | 	} | 
 | 126 |  | 
| Ivan Lozano | 829e1e9 | 2023-10-09 11:30:09 -0400 | [diff] [blame] | 127 | 	if b.ClangProperties.Cpp_std != nil { | 
 | 128 | 		isCpp = true | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 129 | 		if String(b.ClangProperties.Cpp_std) == "experimental" { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 130 | 			stdVersion = cc_config.ExperimentalCppStdVersion | 
| Ivan Lozano | 829e1e9 | 2023-10-09 11:30:09 -0400 | [diff] [blame] | 131 | 		} else if String(b.ClangProperties.Cpp_std) == "default" || String(b.ClangProperties.Cpp_std) == "" { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 132 | 			stdVersion = cc_config.CppStdVersion | 
 | 133 | 		} else { | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 134 | 			stdVersion = String(b.ClangProperties.Cpp_std) | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 135 | 		} | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 136 | 	} else if b.ClangProperties.C_std != nil { | 
| Ivan Lozano | 829e1e9 | 2023-10-09 11:30:09 -0400 | [diff] [blame] | 137 | 		isCpp = false | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 138 | 		if String(b.ClangProperties.C_std) == "experimental" { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 139 | 			stdVersion = cc_config.ExperimentalCStdVersion | 
| Ivan Lozano | 829e1e9 | 2023-10-09 11:30:09 -0400 | [diff] [blame] | 140 | 		} else if String(b.ClangProperties.C_std) == "default" || String(b.ClangProperties.C_std) == "" { | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 141 | 			stdVersion = cc_config.CStdVersion | 
 | 142 | 		} else { | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 143 | 			stdVersion = String(b.ClangProperties.C_std) | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 144 | 		} | 
 | 145 | 	} else if isCpp { | 
 | 146 | 		stdVersion = cc_config.CppStdVersion | 
 | 147 | 	} else { | 
 | 148 | 		stdVersion = cc_config.CStdVersion | 
 | 149 | 	} | 
 | 150 |  | 
 | 151 | 	return stdVersion, isCpp | 
 | 152 | } | 
 | 153 |  | 
| Thiébaud Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 154 | func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { | 
 | 155 | 	ccToolchain := ctx.RustModule().ccToolchain(ctx) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 156 |  | 
 | 157 | 	var cflags []string | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 158 | 	var implicits android.Paths | 
 | 159 |  | 
| Ivan Lozano | ddd0bdb | 2020-08-28 17:00:26 -0400 | [diff] [blame] | 160 | 	implicits = append(implicits, deps.depGeneratedHeaders...) | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 161 |  | 
 | 162 | 	// Default clang flags | 
| Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 163 | 	cflags = append(cflags, "${cc_config.CommonGlobalCflags}") | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 164 | 	if ctx.Device() { | 
| Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 165 | 		cflags = append(cflags, "${cc_config.DeviceGlobalCflags}") | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 166 | 	} | 
 | 167 |  | 
 | 168 | 	// Toolchain clang flags | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 169 | 	cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) | 
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 170 | 	cflags = append(cflags, strings.ReplaceAll(ccToolchain.Cflags(), "${config.", "${cc_config.")) | 
 | 171 | 	cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainCflags(), "${config.", "${cc_config.")) | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 172 |  | 
| Ivan Lozano | e950cda | 2021-11-09 11:26:04 -0500 | [diff] [blame] | 173 | 	if ctx.RustModule().UseVndk() { | 
 | 174 | 		cflags = append(cflags, "-D__ANDROID_VNDK__") | 
 | 175 | 		if ctx.RustModule().InVendor() { | 
 | 176 | 			cflags = append(cflags, "-D__ANDROID_VENDOR__") | 
 | 177 | 		} else if ctx.RustModule().InProduct() { | 
 | 178 | 			cflags = append(cflags, "-D__ANDROID_PRODUCT__") | 
 | 179 | 		} | 
 | 180 | 	} | 
 | 181 |  | 
 | 182 | 	if ctx.RustModule().InRecovery() { | 
 | 183 | 		cflags = append(cflags, "-D__ANDROID_RECOVERY__") | 
 | 184 | 	} | 
 | 185 |  | 
 | 186 | 	if mctx, ok := ctx.(*moduleContext); ok && mctx.apexVariationName() != "" { | 
 | 187 | 		cflags = append(cflags, "-D__ANDROID_APEX__") | 
| Ivan Lozano | e950cda | 2021-11-09 11:26:04 -0500 | [diff] [blame] | 188 | 	} | 
 | 189 |  | 
 | 190 | 	if ctx.Target().NativeBridge == android.NativeBridgeEnabled { | 
 | 191 | 		cflags = append(cflags, "-D__ANDROID_NATIVE_BRIDGE__") | 
 | 192 | 	} | 
 | 193 |  | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 194 | 	// Dependency clang flags and include paths | 
 | 195 | 	cflags = append(cflags, deps.depClangFlags...) | 
 | 196 | 	for _, include := range deps.depIncludePaths { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 197 | 		cflags = append(cflags, "-I"+include.String()) | 
 | 198 | 	} | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 199 | 	for _, include := range deps.depSystemIncludePaths { | 
 | 200 | 		cflags = append(cflags, "-isystem "+include.String()) | 
 | 201 | 	} | 
 | 202 |  | 
| Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 203 | 	esc := proptools.NinjaAndShellEscapeList | 
 | 204 |  | 
| Ivan Lozano | 0a2a115 | 2020-10-16 10:49:08 -0400 | [diff] [blame] | 205 | 	// Filter out invalid cflags | 
 | 206 | 	for _, flag := range b.ClangProperties.Cflags { | 
 | 207 | 		if flag == "-x c++" || flag == "-xc++" { | 
 | 208 | 			ctx.PropertyErrorf("cflags", | 
 | 209 | 				"-x c++ should not be specified in cflags; setting cpp_std specifies this is a C++ header, or change the file extension to '.hpp' or '.hh'") | 
 | 210 | 		} | 
 | 211 | 		if strings.HasPrefix(flag, "-std=") { | 
 | 212 | 			ctx.PropertyErrorf("cflags", | 
 | 213 | 				"-std should not be specified in cflags; instead use c_std or cpp_std") | 
 | 214 | 		} | 
 | 215 | 	} | 
 | 216 |  | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 217 | 	// Module defined clang flags and include paths | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 218 | 	cflags = append(cflags, esc(b.ClangProperties.Cflags)...) | 
 | 219 | 	for _, include := range b.ClangProperties.Local_include_dirs { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 220 | 		cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 221 | 		implicits = append(implicits, android.PathForModuleSrc(ctx, include)) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 222 | 	} | 
 | 223 |  | 
 | 224 | 	bindgenFlags := defaultBindgenFlags | 
| Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 225 | 	bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 226 |  | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 227 | 	// cat reads from stdin if its command line is empty, | 
 | 228 | 	// so we pass in /dev/null if there are no other flag files | 
 | 229 | 	bindgenFlagFiles := []string{"/dev/null"} | 
 | 230 | 	for _, flagFile := range b.Properties.Bindgen_flag_files { | 
 | 231 | 		bindgenFlagFiles = append(bindgenFlagFiles, android.PathForModuleSrc(ctx, flagFile).String()) | 
 | 232 | 		implicits = append(implicits, android.PathForModuleSrc(ctx, flagFile)) | 
 | 233 | 	} | 
 | 234 |  | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 235 | 	wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src) | 
 | 236 | 	if !wrapperFile.Valid() { | 
 | 237 | 		ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source") | 
 | 238 | 	} | 
 | 239 |  | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 240 | 	// Add C std version flag | 
 | 241 | 	stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path()) | 
 | 242 | 	cflags = append(cflags, "-std="+stdVersion) | 
 | 243 |  | 
 | 244 | 	// Specify the header source language to avoid ambiguity. | 
 | 245 | 	if isCpp { | 
 | 246 | 		cflags = append(cflags, "-x c++") | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 247 | 		// Add any C++ only flags. | 
 | 248 | 		cflags = append(cflags, esc(b.ClangProperties.Cppflags)...) | 
| Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 249 | 	} else { | 
 | 250 | 		cflags = append(cflags, "-x c") | 
 | 251 | 	} | 
 | 252 |  | 
| Colin Cross | 8a5a1e2 | 2022-10-14 15:53:02 -0700 | [diff] [blame] | 253 | 	// clang-r468909b complains about the -x c in the flags in clang-sys parse_search_paths: | 
 | 254 | 	// clang: error: '-x c' after last input file has no effect [-Werror,-Wunused-command-line-argument] | 
 | 255 | 	cflags = append(cflags, "-Wno-unused-command-line-argument") | 
 | 256 |  | 
| Yi Kong | 4f664e9 | 2022-07-14 20:36:15 +0800 | [diff] [blame] | 257 | 	// LLVM_NEXT may contain flags that bindgen doesn't recognise. Turn off unknown flags warning. | 
 | 258 | 	if ctx.Config().IsEnvTrue("LLVM_NEXT") { | 
 | 259 | 		cflags = append(cflags, "-Wno-unknown-warning-option") | 
 | 260 | 	} | 
 | 261 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 262 | 	outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 263 |  | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 264 | 	var cmd, cmdDesc string | 
 | 265 | 	if b.Properties.Custom_bindgen != "" { | 
| Andrei Homescu | 4494685 | 2023-07-07 04:59:07 +0000 | [diff] [blame] | 266 | 		cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(android.HostToolProvider).HostToolPath().String() | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 267 | 		cmdDesc = b.Properties.Custom_bindgen | 
 | 268 | 	} else { | 
 | 269 | 		cmd = "$bindgenCmd" | 
 | 270 | 		cmdDesc = "bindgen" | 
 | 271 | 	} | 
 | 272 |  | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 273 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 274 | 		Rule:        bindgen, | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 275 | 		Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "), | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 276 | 		Output:      outputFile, | 
 | 277 | 		Input:       wrapperFile.Path(), | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 278 | 		Implicits:   implicits, | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 279 | 		Args: map[string]string{ | 
| Andrei Homescu | 78a1f8d | 2023-07-08 00:35:15 +0000 | [diff] [blame] | 280 | 			"cmd":       cmd, | 
 | 281 | 			"flags":     strings.Join(bindgenFlags, " "), | 
 | 282 | 			"flagfiles": strings.Join(bindgenFlagFiles, " "), | 
 | 283 | 			"cflags":    strings.Join(cflags, " "), | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 284 | 		}, | 
 | 285 | 	}) | 
| Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 286 |  | 
| Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 287 | 	b.BaseSourceProvider.OutputFiles = android.Paths{outputFile} | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 288 | 	return outputFile | 
 | 289 | } | 
 | 290 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 291 | func (b *bindgenDecorator) SourceProviderProps() []interface{} { | 
 | 292 | 	return append(b.BaseSourceProvider.SourceProviderProps(), | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 293 | 		&b.Properties, &b.ClangProperties) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 294 | } | 
 | 295 |  | 
| Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 296 | // rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input. | 
 | 297 | // Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 298 | // the header and generated source is appropriately handled. It is recommended to add it as a dependency in the | 
| Vinh Tran | 4eeb2a9 | 2023-08-14 13:29:30 -0400 | [diff] [blame] | 299 | // rlibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":" | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 300 | // prefix. | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 301 | func RustBindgenFactory() android.Module { | 
 | 302 | 	module, _ := NewRustBindgen(android.HostAndDeviceSupported) | 
 | 303 | 	return module.Init() | 
 | 304 | } | 
 | 305 |  | 
| Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 306 | func RustBindgenHostFactory() android.Module { | 
 | 307 | 	module, _ := NewRustBindgen(android.HostSupported) | 
 | 308 | 	return module.Init() | 
 | 309 | } | 
 | 310 |  | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 311 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 312 | 	bindgen := &bindgenDecorator{ | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 313 | 		BaseSourceProvider: NewSourceProvider(), | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 314 | 		Properties:         BindgenProperties{}, | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 315 | 		ClangProperties:    cc.RustBindgenClangProperties{}, | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 316 | 	} | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 317 |  | 
| Matthew Maurer | e94f3e7 | 2022-08-10 20:25:50 +0000 | [diff] [blame] | 318 | 	module := NewSourceProviderModule(hod, bindgen, false, true) | 
 | 319 |  | 
 | 320 | 	android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
 | 321 | 		type stub_props struct { | 
 | 322 | 			Visibility []string | 
 | 323 | 		} | 
 | 324 | 		props := &stub_props{[]string{":__subpackages__"}} | 
 | 325 | 		ctx.PrependProperties(props) | 
 | 326 | 	}) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 327 |  | 
 | 328 | 	return module, bindgen | 
 | 329 | } | 
 | 330 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 331 | func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { | 
 | 332 | 	deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps) | 
| Dan Albert | 30e66dc | 2023-02-23 19:17:50 +0000 | [diff] [blame] | 333 | 	if ctx.toolchain().Bionic() && !ctx.RustModule().compiler.noStdlibs() { | 
| Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 334 | 		deps = bionicDeps(ctx, deps, false) | 
| Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 335 | 	} else if ctx.Os() == android.LinuxMusl { | 
 | 336 | 		deps = muslDeps(ctx, deps, false) | 
| Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 337 | 	} | 
 | 338 |  | 
| Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 339 | 	deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...) | 
 | 340 | 	deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...) | 
| Ivan Lozano | 9b44383 | 2020-11-17 13:39:30 -0500 | [diff] [blame] | 341 | 	deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 342 | 	return deps | 
 | 343 | } |