| 1 |
|
| 2 |
|
| 3 | always-$(CONFIG_RUST) += target.json
|
| 4 | no-clean-files += target.json
|
| 5 |
|
| 6 | obj-$(CONFIG_RUST) += core.o compiler_builtins.o
|
| 7 | always-$(CONFIG_RUST) += exports_core_generated.h
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 | obj-$(CONFIG_RUST) += helpers.o
|
| 12 | CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
|
| 13 |
|
| 14 | always-$(CONFIG_RUST) += libmacros.so
|
| 15 | no-clean-files += libmacros.so
|
| 16 |
|
| 17 | always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
|
| 18 | obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
|
| 19 | always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
|
| 20 | exports_kernel_generated.h
|
| 21 |
|
| 22 | ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
|
| 23 | obj-$(CONFIG_RUST) += build_error.o
|
| 24 | else
|
| 25 | always-$(CONFIG_RUST) += build_error.o
|
| 26 | endif
|
| 27 |
|
| 28 | obj-$(CONFIG_RUST) += exports.o
|
| 29 |
|
| 30 |
|
| 31 | ifdef CONFIG_RUST
|
| 32 |
|
| 33 |
|
| 34 | rustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
|
| 35 | rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
|
| 36 | RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
|
| 37 |
|
| 38 | ifeq ($(quiet),silent_)
|
| 39 | cargo_quiet=-q
|
| 40 | rust_test_quiet=-q
|
| 41 | rustdoc_test_quiet=--test-args -q
|
| 42 | else ifeq ($(quiet),quiet_)
|
| 43 | rust_test_quiet=-q
|
| 44 | rustdoc_test_quiet=--test-args -q
|
| 45 | else
|
| 46 | cargo_quiet=--verbose
|
| 47 | endif
|
| 48 |
|
| 49 | core-cfgs = \
|
| 50 | --cfg no_fp_fmt_parse
|
| 51 |
|
| 52 | alloc-cfgs = \
|
| 53 | --cfg no_fmt \
|
| 54 | --cfg no_global_oom_handling \
|
| 55 | --cfg no_macros \
|
| 56 | --cfg no_rc \
|
| 57 | --cfg no_str \
|
| 58 | --cfg no_string \
|
| 59 | --cfg no_sync \
|
| 60 | --cfg no_thin
|
| 61 |
|
| 62 | quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
|
| 63 | cmd_rustdoc = \
|
| 64 | OBJTREE=$(abspath $(objtree)) \
|
| 65 | $(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
|
| 66 | $(rustc_target_flags) -L$(objtree)/$(obj) \
|
| 67 | --output $(objtree)/$(obj)/doc \
|
| 68 | --crate-name $(subst rustdoc-,,$@) \
|
| 69 | @$(objtree)/include/generated/rustc_cfg $<
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 | rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
|
| 83 | rustdoc-alloc rustdoc-kernel
|
| 84 | $(Q)cp $(srctree)/Documentation/images/logo.svg $(objtree)/$(obj)/doc
|
| 85 | $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(objtree)/$(obj)/doc
|
| 86 | $(Q)find $(objtree)/$(obj)/doc -name '*.html' -type f -print0 | xargs -0 sed -Ei \
|
| 87 | -e 's:rust-logo\.svg:logo.svg:g' \
|
| 88 | -e 's:rust-logo\.png:logo.svg:g' \
|
| 89 | -e 's:favicon\.svg:logo.svg:g' \
|
| 90 | -e 's:<link rel="alternate icon" type="image/png" href="[./]*favicon-(16x16|32x32)\.png">::g'
|
| 91 | $(Q)echo '.logo-container > img { object-fit: contain; }' \
|
| 92 | >> $(objtree)/$(obj)/doc/rustdoc.css
|
| 93 |
|
| 94 | rustdoc-macros: private rustdoc_host = yes
|
| 95 | rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
|
| 96 | --extern proc_macro
|
| 97 | rustdoc-macros: $(src)/macros/lib.rs FORCE
|
| 98 | $(call if_changed,rustdoc)
|
| 99 |
|
| 100 | rustdoc-core: private rustc_target_flags = $(core-cfgs)
|
| 101 | rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
|
| 102 | $(call if_changed,rustdoc)
|
| 103 |
|
| 104 | rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
|
| 105 | $(call if_changed,rustdoc)
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 | rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
|
| 112 | -Arustdoc::broken_intra_doc_links
|
| 113 | rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
|
| 114 | $(call if_changed,rustdoc)
|
| 115 |
|
| 116 | rustdoc-kernel: private rustc_target_flags = --extern alloc \
|
| 117 | --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
|
| 118 | --extern bindings
|
| 119 | rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
|
| 120 | rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
|
| 121 | $(obj)/bindings.o FORCE
|
| 122 | $(call if_changed,rustdoc)
|
| 123 |
|
| 124 | quiet_cmd_rustc_test_library = RUSTC TL $<
|
| 125 | cmd_rustc_test_library = \
|
| 126 | OBJTREE=$(abspath $(objtree)) \
|
| 127 | $(RUSTC) $(rust_common_flags) \
|
| 128 | @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
|
| 129 | --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
|
| 130 | --out-dir $(objtree)/$(obj)/test --cfg testlib \
|
| 131 | --sysroot $(objtree)/$(obj)/test/sysroot \
|
| 132 | -L$(objtree)/$(obj)/test \
|
| 133 | --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
|
| 134 |
|
| 135 | rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
|
| 136 | $(call if_changed,rustc_test_library)
|
| 137 |
|
| 138 | rusttestlib-macros: private rustc_target_flags = --extern proc_macro
|
| 139 | rusttestlib-macros: private rustc_test_library_proc = yes
|
| 140 | rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
|
| 141 | $(call if_changed,rustc_test_library)
|
| 142 |
|
| 143 | rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
|
| 144 | $(call if_changed,rustc_test_library)
|
| 145 |
|
| 146 | quiet_cmd_rustdoc_test = RUSTDOC T $<
|
| 147 | cmd_rustdoc_test = \
|
| 148 | OBJTREE=$(abspath $(objtree)) \
|
| 149 | $(RUSTDOC) --test $(rust_common_flags) \
|
| 150 | @$(objtree)/include/generated/rustc_cfg \
|
| 151 | $(rustc_target_flags) $(rustdoc_test_target_flags) \
|
| 152 | --sysroot $(objtree)/$(obj)/test/sysroot $(rustdoc_test_quiet) \
|
| 153 | -L$(objtree)/$(obj)/test --output $(objtree)/$(obj)/doc \
|
| 154 | --crate-name $(subst rusttest-,,$@) $<
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 | quiet_cmd_rustc_test = RUSTC T $<
|
| 159 | cmd_rustc_test = \
|
| 160 | OBJTREE=$(abspath $(objtree)) \
|
| 161 | $(RUSTC) --test $(rust_common_flags) \
|
| 162 | @$(objtree)/include/generated/rustc_cfg \
|
| 163 | $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
|
| 164 | --sysroot $(objtree)/$(obj)/test/sysroot \
|
| 165 | -L$(objtree)/$(obj)/test \
|
| 166 | --crate-name $(subst rusttest-,,$@) $<; \
|
| 167 | $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
|
| 168 | $(rustc_test_run_flags)
|
| 169 |
|
| 170 | rusttest: rusttest-macros rusttest-kernel
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 | quiet_cmd_rustsysroot = RUSTSYSROOT
|
| 200 | cmd_rustsysroot = \
|
| 201 | rm -rf $(objtree)/$(obj)/test; \
|
| 202 | mkdir -p $(objtree)/$(obj)/test; \
|
| 203 | cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \
|
| 204 | cp -r $(srctree)/$(src)/alloc/* \
|
| 205 | $(objtree)/$(obj)/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \
|
| 206 | echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \
|
| 207 | echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \
|
| 208 | >> $(objtree)/$(obj)/test/rustc_sysroot; \
|
| 209 | chmod u+x $(objtree)/$(obj)/test/rustc_sysroot; \
|
| 210 | $(CARGO) -q new $(objtree)/$(obj)/test/dummy; \
|
| 211 | RUSTC=$(objtree)/$(obj)/test/rustc_sysroot $(CARGO) $(cargo_quiet) \
|
| 212 | test -Zbuild-std --target $(rustc_host_target) \
|
| 213 | --manifest-path $(objtree)/$(obj)/test/dummy/Cargo.toml; \
|
| 214 | rm $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \
|
| 215 | cp $(objtree)/$(obj)/test/dummy/target/$(rustc_host_target)/debug/deps/* \
|
| 216 | $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
|
| 217 |
|
| 218 | rusttest-prepare: FORCE
|
| 219 | $(call if_changed,rustsysroot)
|
| 220 |
|
| 221 | rusttest-macros: private rustc_target_flags = --extern proc_macro
|
| 222 | rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
|
| 223 | rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
|
| 224 | $(call if_changed,rustc_test)
|
| 225 | $(call if_changed,rustdoc_test)
|
| 226 |
|
| 227 | rusttest-kernel: private rustc_target_flags = --extern alloc \
|
| 228 | --extern build_error --extern macros --extern bindings
|
| 229 | rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
|
| 230 | rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
|
| 231 | $(call if_changed,rustc_test)
|
| 232 | $(call if_changed,rustc_test_library)
|
| 233 |
|
| 234 | filechk_rust_target = $(objtree)/scripts/generate_rust_target < $<
|
| 235 |
|
| 236 | $(obj)/target.json: $(objtree)/include/config/auto.conf FORCE
|
| 237 | $(call filechk,rust_target)
|
| 238 |
|
| 239 | ifdef CONFIG_CC_IS_CLANG
|
| 240 | bindgen_c_flags = $(c_flags)
|
| 241 | else
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 | bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
|
| 249 | -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
|
| 250 | -mindirect-branch=thunk-extern -mindirect-branch-register \
|
| 251 | -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
|
| 252 | -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
|
| 253 | -mno-pointers-to-nested-functions -mno-string \
|
| 254 | -mno-strict-align -mstrict-align \
|
| 255 | -fconserve-stack -falign-jumps=% -falign-loops=% \
|
| 256 | -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
|
| 257 | -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
|
| 258 | -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
|
| 259 | -fzero-call-used-regs=% -fno-stack-clash-protection \
|
| 260 | -fno-inline-functions-called-once \
|
| 261 | --param=% --param asan-%
|
| 262 |
|
| 263 |
|
| 264 | BINDGEN_TARGET_x86 := x86_64-linux-gnu
|
| 265 | BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 | bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
|
| 271 |
|
| 272 | bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
|
| 273 | $(bindgen_extra_c_flags)
|
| 274 | endif
|
| 275 |
|
| 276 | ifdef CONFIG_LTO
|
| 277 | bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
|
| 278 | else
|
| 279 | bindgen_c_flags_lto = $(bindgen_c_flags)
|
| 280 | endif
|
| 281 |
|
| 282 | bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
|
| 283 |
|
| 284 | quiet_cmd_bindgen = BINDGEN $@
|
| 285 | cmd_bindgen = \
|
| 286 | $(BINDGEN) $< $(bindgen_target_flags) \
|
| 287 | --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
|
| 288 | --no-debug '.*' \
|
| 289 | --size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
|
| 290 | $(bindgen_target_cflags) $(bindgen_target_extra)
|
| 291 |
|
| 292 | $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
|
| 293 | $(shell grep -v '^\#\|^$$' $(srctree)/$(src)/bindgen_parameters)
|
| 294 | $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
|
| 295 | $(src)/bindgen_parameters FORCE
|
| 296 | $(call if_changed_dep,bindgen)
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 | $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
|
| 303 | --blacklist-type '.*' --whitelist-var '' \
|
| 304 | --whitelist-function 'rust_helper_.*'
|
| 305 | $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
|
| 306 | -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
|
| 307 | $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
|
| 308 | sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/
|
| 309 | $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
|
| 310 | $(call if_changed_dep,bindgen)
|
| 311 |
|
| 312 | quiet_cmd_exports = EXPORTS $@
|
| 313 | cmd_exports = \
|
| 314 | $(NM) -p --defined-only $< \
|
| 315 | | grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
|
| 316 | | xargs -Isymbol \
|
| 317 | echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
|
| 318 |
|
| 319 | $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
|
| 320 | $(call if_changed,exports)
|
| 321 |
|
| 322 | $(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
|
| 323 | $(call if_changed,exports)
|
| 324 |
|
| 325 | $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
|
| 326 | $(call if_changed,exports)
|
| 327 |
|
| 328 | $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
|
| 329 | $(call if_changed,exports)
|
| 330 |
|
| 331 | quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
|
| 332 | cmd_rustc_procmacro = \
|
| 333 | $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
|
| 334 | --emit=dep-info,link --extern proc_macro \
|
| 335 | --crate-type proc-macro --out-dir $(objtree)/$(obj) \
|
| 336 | --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \
|
| 337 | mv $(objtree)/$(obj)/$(patsubst lib%.so,%,$(notdir $@)).d $(depfile); \
|
| 338 | sed -i '/^\#/d' $(depfile)
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 | $(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
|
| 344 | $(call if_changed_dep,rustc_procmacro)
|
| 345 |
|
| 346 | quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
|
| 347 | cmd_rustc_library = \
|
| 348 | OBJTREE=$(abspath $(objtree)) \
|
| 349 | $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
|
| 350 | $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
|
| 351 | --emit=dep-info,obj,metadata --crate-type rlib \
|
| 352 | --out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
|
| 353 | --crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
|
| 354 | mv $(objtree)/$(obj)/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \
|
| 355 | sed -i '/^\#/d' $(depfile) \
|
| 356 | $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
|
| 357 |
|
| 358 | rust-analyzer:
|
| 359 | $(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
|
| 360 | $(RUST_LIB_SRC) > $(objtree)/rust-project.json
|
| 361 |
|
| 362 | $(obj)/core.o: private skip_clippy = 1
|
| 363 | $(obj)/core.o: private skip_flags = -Dunreachable_pub
|
| 364 | $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
|
| 365 | $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs $(obj)/target.json FORCE
|
| 366 | $(call if_changed_dep,rustc_library)
|
| 367 |
|
| 368 | $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
|
| 369 | $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
|
| 370 | $(call if_changed_dep,rustc_library)
|
| 371 |
|
| 372 | $(obj)/alloc.o: private skip_clippy = 1
|
| 373 | $(obj)/alloc.o: private skip_flags = -Dunreachable_pub
|
| 374 | $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
|
| 375 | $(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
|
| 376 | $(call if_changed_dep,rustc_library)
|
| 377 |
|
| 378 | $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
|
| 379 | $(call if_changed_dep,rustc_library)
|
| 380 |
|
| 381 | $(obj)/bindings.o: $(src)/bindings/lib.rs \
|
| 382 | $(obj)/compiler_builtins.o \
|
| 383 | $(obj)/bindings/bindings_generated.rs \
|
| 384 | $(obj)/bindings/bindings_helpers_generated.rs FORCE
|
| 385 | $(call if_changed_dep,rustc_library)
|
| 386 |
|
| 387 | $(obj)/kernel.o: private rustc_target_flags = --extern alloc \
|
| 388 | --extern build_error --extern macros --extern bindings
|
| 389 | $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
|
| 390 | $(obj)/libmacros.so $(obj)/bindings.o FORCE
|
| 391 | $(call if_changed_dep,rustc_library)
|
| 392 |
|
| 393 | endif
|