From patchwork Sat Nov 4 21:11:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Rodriguez Reboredo X-Patchwork-Id: 13445609 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 334C9C4332F for ; Sat, 4 Nov 2023 21:12:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229788AbjKDVMa (ORCPT ); Sat, 4 Nov 2023 17:12:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjKDVM3 (ORCPT ); Sat, 4 Nov 2023 17:12:29 -0400 Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84266D69; Sat, 4 Nov 2023 14:12:26 -0700 (PDT) Received: by mail-yb1-xb2d.google.com with SMTP id 3f1490d57ef6-da30fd994fdso3736796276.1; Sat, 04 Nov 2023 14:12:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699132345; x=1699737145; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l1+YL2Yjacvz8rzAR5vnISqtQY7Ls7DpSWKcTQHzMSQ=; b=i2aWaG2o+CRu1OkOeQLD+Hb3SuYGihptEcnHLdWXAYpsnnJCgAfPcfDqfoMqWsThAQ 9a51b+YxZNIIwagrt41xmVDovYTGXdV91hLY3GQpVQYGlLRjEBRJu4vADp8CcueuICUc ad5/+FUEPiFx6bvsOSjOhHhs02bUhPWoJnnD19k7mUorskxcbK2x2nrhyEnTvIkb9vii 7QVpbVG34df/Eqgm/upqVxVOoT4J1FvE9AXv69j+GEQ7+XvdpCW+FtYZQDJ8gSnkREAN QPhq2GMv3Grjq1sd5oao+B+7aCE3fbcHP92c6/9CyT70ep9sL9I7yXV56G/yhhaXK7tw KtkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699132345; x=1699737145; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l1+YL2Yjacvz8rzAR5vnISqtQY7Ls7DpSWKcTQHzMSQ=; b=evd30A3a77a12kehSC0aOP3fokRNmQXvmobvw1cp1yfE93R79Ty8+OB1FJsdg/wHC4 58OpJ3AsVcy4RjK78wkFsG94SLwocX3f0Cmu670m9j3o6dbQU85BTP4wy8rbcy5IvCAP hyoRYu7sYgEb0Rcpxcra/bdAwDIRpfsJADWKcC/o0bacGIjSmLy5yLVexNZPdoLYYLQv GVfly2ULWDFvuSr/oPvYUdkO8horris0Tr81eW7hUiiY4KR0mwjGpEDE2N8yDTCX2iYs xxzf2VcBkew4LHdR1T5Q4KNykQfGTnR41S/aNG1uUL5yredDWBhccJ2u1NfEE/uBqyaW /FOg== X-Gm-Message-State: AOJu0YxmwZJa9u0IQjDfy3z+M9lH5Dnr0jldkjQNUab2QWGKU8YYl5eg 8OaREAP19wqL6b9bctLA17I= X-Google-Smtp-Source: AGHT+IGT6zFASbTC5oiNFGpQkU5Z6grGCh7qN+2jrKjvJjatE+zHucrS2QSae2CfsLRzV1qoae5yrA== X-Received: by 2002:a25:1507:0:b0:da0:aa1b:80f3 with SMTP id 7-20020a251507000000b00da0aa1b80f3mr5050646ybv.10.1699132345601; Sat, 04 Nov 2023 14:12:25 -0700 (PDT) Received: from tx3000mach.io (static.220.238.itcsa.net. [190.15.220.238]) by smtp.gmail.com with ESMTPSA id t14-20020a25838e000000b00d9cc49edae9sm2094731ybk.63.2023.11.04.14.12.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 14:12:25 -0700 (PDT) From: Martin Rodriguez Reboredo To: Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , David Gow , Shuah Khan , Andrew Davis , Andy Shevchenko , Chuck Lever Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [RFC PATCH v3 1/2] kbuild: Build Rust crates as libraries Date: Sat, 4 Nov 2023 18:11:58 -0300 Message-ID: <20231104211213.225891-2-yakoyoku@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231104211213.225891-1-yakoyoku@gmail.com> References: <20231104211213.225891-1-yakoyoku@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Enables compiling Rust crates as dependencies of kernel modules. These are also meant to be used as libraries for other parts of the kernel. For these crates `bindings` is also exposed to them and they get their symbols exported for them to be used by other code. When a composite object depends on an `.rlib` file, which by the way is a current ar archive, Kbuild will compile it from its base Rust source and archive it. This makes possible to have Rust bindings for a subsystem that is compiled either built-in or as a module. They can also be made into modules by themselves too. Signed-off-by: Martin Rodriguez Reboredo --- v2 -> v3: - Fixed erroneous dependency on exported symbols from KUnit tests. v1 -> v2: - Fixed builtins compilation. - Added support for building crates as modules. .gitignore | 2 ++ Makefile | 4 ++-- scripts/Makefile.build | 42 ++++++++++++++++++++++++++++++++++++--- scripts/Makefile.lib | 20 +++++++++++++++---- scripts/Makefile.modfinal | 9 +++++++-- 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 0bbae167bf93..8353b01e2915 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ *.o *.o.* *.patch +*.rlib *.rmeta *.rpm *.rsi @@ -53,6 +54,7 @@ *.zst Module.symvers modules.order +exports_*_generated.c # # Top-level generic files diff --git a/Makefile b/Makefile index 8a40530868ff..4113a5b93ddc 100644 --- a/Makefile +++ b/Makefile @@ -283,7 +283,7 @@ no-compiler-targets := $(no-dot-config-targets) install dtbs_install \ headers_install modules_install modules_sign kernelrelease image_name no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \ image_name -single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %.symtypes %/ +single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rlib %.rsi %.s %.symtypes %/ config-build := mixed-build := @@ -1919,7 +1919,7 @@ $(clean-dirs): clean: $(clean-dirs) $(call cmd,rmfiles) @find $(or $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \ - \( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ + \( -name '*.[aios]' -o -name '*.rlib' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ -o -name '*.ko.*' \ -o -name '*.dtb' -o -name '*.dtbo' \ -o -name '*.dtb.S' -o -name '*.dtbo.S' \ diff --git a/scripts/Makefile.build b/scripts/Makefile.build index da37bfa97211..d31b4272a79f 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -20,6 +20,7 @@ always-m := targets := subdir-y := subdir-m := +rust-libs := EXTRA_AFLAGS := EXTRA_CFLAGS := EXTRA_CPPFLAGS := @@ -274,7 +275,7 @@ rust_common_cmd = \ -Zcrate-attr='feature($(rust_allowed_features))' \ --extern alloc --extern kernel \ --crate-type rlib -L $(objtree)/rust/ \ - --crate-name $(basename $(notdir $@)) \ + --crate-name $(basename $(notdir $<)) \ --out-dir $(dir $@) --emit=dep-info=$(depfile) # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit @@ -285,11 +286,41 @@ rust_common_cmd = \ # i.e. the outputs we would get for the different single targets (e.g. `.ll`) # would not match each other. +rlib_obj = $(dir $@)$*.$*.o +rlib_meta = $(dir $@)$(patsubst %.rlib,%.rmeta,$(notdir $@)) + quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< +quiet_cmd_ln_rlib_o = SYMLINK $@ + cmd_ln_rlib_o = ln -s -f $*.$*.o $@ + $(obj)/%.o: $(src)/%.rs FORCE - $(call if_changed_dep,rustc_o_rs) + $(if $(findstring $@,$(crate-obj-m)), \ + $(call if_changed,ln_rlib_o), $(call if_changed_dep,rustc_o_rs)) + +quiet_cmd_rustc_rlib_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ + cmd_rustc_rlib_rs = $(rust_common_cmd) --extern bindings \ + --emit=obj=$(rlib_obj) --emit=metadata=$(rlib_meta) $<; \ + rm -f $@; $(AR) cDPrST $@ $(rlib_obj) $(rlib_meta) + +quiet_cmd_rlib_exports = EXPORTS $@ + cmd_rlib_exports = \ + echo "\#include " > $@; \ + $(NM) -p --defined-only $(rlib_obj) \ + | perl -ae '/ (T|R|D) (?!(init|cleanup)_module)/ and' \ + -e 'print "extern int @F[2]; EXPORT_SYMBOL_GPL(@F[2]);"' >> $@ + +$(crate-obj-m): $(obj)/%.o: $(obj)/exports_%_generated.o $(obj)/lib%.rlib $(src)/%.rs FORCE + +$(obj)/lib%.rlib: $(src)/%.rs FORCE + $(call if_changed_dep,rustc_rlib_rs) + +$(obj)/exports_%_generated.c: $(obj)/lib%.rlib FORCE + $(call if_changed,rlib_exports) + +$(obj)/exports_%_generated.o: $(obj)/exports_%_generated.c FORCE + $(call if_changed,cc_o_c) quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ cmd_rustc_rsi_rs = \ @@ -394,9 +425,14 @@ $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; # To make this rule robust against "Argument list too long" error, # remove $(obj)/ prefix, and restore it by a shell command. +# If we have a .rlib for the archive change it for its object member +crate-obj = $(foreach m, $1, $(if $(findstring .rlib, $m), \ + $(basename $(m:lib%=%)).$(m:lib%.rlib=%.o),$m)) + quiet_cmd_ar_builtin = AR $@ cmd_ar_builtin = rm -f $@; \ - $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \ + $(if $(real-prereqs), printf "$(obj)/%s " \ + $(call crate-obj, $(patsubst $(obj)/%,%,$(real-prereqs))) | xargs) \ $(AR) cDPrST $@ $(obj)/built-in.a: $(real-obj-y) FORCE diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 68d0134bdbf9..3fe419250dbd 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -45,12 +45,17 @@ else obj-y := $(filter-out %/, $(obj-y)) endif +crate-obj-y := $(foreach m, $(filter lib%.rlib, $(obj-y)), $(m:lib%.rlib=%.o)) +crate-obj-m := $(foreach m, $(filter lib%.rlib, $(obj-m)), $(m:lib%.rlib=%.o)) + # Expand $(foo-objs) $(foo-y) etc. by replacing their individuals suffix-search = $(strip $(foreach s, $3, $($(1:%$(strip $2)=%$s)))) # List composite targets that are constructed by combining other targets multi-search = $(sort $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $m))) # List primitive targets that are compiled from source files real-search = $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $(call suffix-search, $m, $2, $3), $m)) +# List exported symbols from .rlib targets +rlib-exports = $(foreach m, $1, $(if $(findstring .rlib, $m),$(m:lib%.rlib=exports_%_generated.o)) $m) # If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object multi-obj-y := $(call multi-search, $(obj-y), .o, -objs -y) @@ -59,8 +64,8 @@ multi-obj-ym := $(multi-obj-y) $(multi-obj-m) # Replace multi-part objects by their individual parts, # including built-in.a from subdirectories -real-obj-y := $(call real-search, $(obj-y), .o, -objs -y) -real-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m) +real-obj-y := $(call rlib-exports, $(call real-search, $(obj-y), .o, -objs -y)) +real-obj-m := $(call rlib-exports, $(call real-search, $(obj-m), .o, -objs -y -m)) always-y += $(always-m) @@ -96,6 +101,8 @@ always-y := $(addprefix $(obj)/,$(always-y)) targets := $(addprefix $(obj)/,$(targets)) obj-m := $(addprefix $(obj)/,$(obj-m)) lib-y := $(addprefix $(obj)/,$(lib-y)) +crate-obj-y := $(addprefix $(obj)/,$(crate-obj-y)) +crate-obj-m := $(addprefix $(obj)/,$(crate-obj-m)) real-obj-y := $(addprefix $(obj)/,$(real-obj-y)) real-obj-m := $(addprefix $(obj)/,$(real-obj-m)) multi-obj-m := $(addprefix $(obj)/, $(multi-obj-m)) @@ -210,7 +217,11 @@ _cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj) endif endif -part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y) +_rust_libs = $(foreach l, $(rust-libs), -L $(obj)/$(dir $l) --extern $(notdir $(basename $l))) + +part-of-module = \ + $(if $(or $(filter $(basename $@).o, $(real-obj-m)), \ + $(filter $(basename $@).rlib, $(real-obj-m))),y) quiet_modtag = $(if $(part-of-module),[M], ) modkern_cflags = \ @@ -232,7 +243,8 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ $(_c_flags) $(modkern_cflags) \ $(basename_flags) $(modname_flags) -rust_flags = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg +rust_flags = $(_rust_flags) $(modkern_rustflags) $(_rust_libs) \ + @$(objtree)/include/generated/rustc_cfg a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ $(_a_flags) $(modkern_aflags) diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index b3a6aa8fbe8c..aab17203f5b1 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -19,7 +19,7 @@ __modfinal: $(modules:%.o=%.ko) @: # modname and part-of-module are set to make c_flags define proper module flags -modname = $(notdir $(@:.mod.o=)) +modname = $(notdir $*) part-of-module = y quiet_cmd_cc_o_c = CC [M] $@ @@ -30,11 +30,16 @@ quiet_cmd_cc_o_c = CC [M] $@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) +# Symlinks of foo.o that point to foo.foo.o are members of a .rlib archives, +# so their exports should be added to the module +ko_objs = $(if $(filter %$(modname).$(modname).o,$(realpath $*.o)),\ + $(dir $*)exports_$(modname)_generated.o) $(filter %.o, $^) + quiet_cmd_ld_ko_o = LD [M] $@ cmd_ld_ko_o += \ $(LD) -r $(KBUILD_LDFLAGS) \ $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ - -T scripts/module.lds -o $@ $(filter %.o, $^); \ + -T scripts/module.lds -o $@ $(ko_objs); \ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) quiet_cmd_btf_ko = BTF [M] $@ From patchwork Sat Nov 4 21:11:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Rodriguez Reboredo X-Patchwork-Id: 13445610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BD3CC4332F for ; Sat, 4 Nov 2023 21:12:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229531AbjKDVMp (ORCPT ); Sat, 4 Nov 2023 17:12:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjKDVMo (ORCPT ); Sat, 4 Nov 2023 17:12:44 -0400 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96D4DD6F; Sat, 4 Nov 2023 14:12:40 -0700 (PDT) Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-d9fe0a598d8so3169852276.2; Sat, 04 Nov 2023 14:12:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699132359; x=1699737159; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NmmxzjjUwK3b8vn2y9x/jblHXMC3dRiI5YQuelEgEu4=; b=Q+vCr0sO6IDgdSXv4wYYodmhCp7JkvqjikNWiahuPEZC/1eDVj49GmSs7wTxElEC04 cVLmGyeMnnu3iByPRRJmk/f4lkgdVRaZLAm+fp3eENvPCkKNMoAz0NnsAjszF6Ctzm6f RpJMUTvKlxshI9gpknKtF+RAphwYq8Xmkj3KgSDD3RZVn0GTFlu9DkEOxSxCaGtJfkPC POGZeL+jLpn6IZFxk/VGSI7z0B6FCHzFJnMWGWr8GEM7TFOLf7yAoZ0vYoRolcoHDXKq uJyzh/WvbGFbLYXYWkvw+4+k49PNKTxxajEdsa7M5wPWbeUB6qbvCMvDEtpFU3MYThKK ykJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699132359; x=1699737159; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NmmxzjjUwK3b8vn2y9x/jblHXMC3dRiI5YQuelEgEu4=; b=ojskqdPwEsM/iiUHyiGtQhPdHg5YZ8iSU63qcZJkCdbwfOqjL18ik0R6YeSBSD12jZ a+4XNygX5EUx9TF01vN0qSPEGn3EAIfUuyDRz0Yg+nyDO55kZQDnprkiQM0ZxRVbJ9cC DwIGPKn/L1vvzQo/jWpcWanO8pSt91Pon4dg9dmXZvKKrE5TCJ5ShLUttHVSB4yiShdr JDhbY8BbDJG6Xu/AD9MpmrFwfHog1gtr/ltP7QZMRqTlr5e0GSxqviTZMyldHT5IL1Uk acUBTOGIsD571vTQnG8TFlo9y/BegWfajl+/LGjxv7/9rJVNE9EkblhkIIQyuaCFGAkh Kc9g== X-Gm-Message-State: AOJu0YwcNebG8QbCSITy6rPcsFiiM9StiElGodZRxpDc0UFhAstcfvt4 p3Z2zDyqdKsp8iOyyrg/CdA= X-Google-Smtp-Source: AGHT+IHp/sOSSBPGc9xRlPjZZ6gFxe6O8jR7w37AYO5PXaa4PLmh0wW/fqWzJ0TWrDwqXjz9SUudGg== X-Received: by 2002:a25:da48:0:b0:d84:e7a6:fc09 with SMTP id n69-20020a25da48000000b00d84e7a6fc09mr24125639ybf.17.1699132359603; Sat, 04 Nov 2023 14:12:39 -0700 (PDT) Received: from tx3000mach.io (static.220.238.itcsa.net. [190.15.220.238]) by smtp.gmail.com with ESMTPSA id t14-20020a25838e000000b00d9cc49edae9sm2094731ybk.63.2023.11.04.14.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 14:12:39 -0700 (PDT) From: Martin Rodriguez Reboredo To: Greg Kroah-Hartman , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, Wedson Almeida Filho , linux-usb@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [RFC PATCH v3 2/2] samples: rust: Add USB sample bindings Date: Sat, 4 Nov 2023 18:11:59 -0300 Message-ID: <20231104211213.225891-3-yakoyoku@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231104211213.225891-1-yakoyoku@gmail.com> References: <20231104211213.225891-1-yakoyoku@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org This is a demonstration of the capabilities of doing bindings with subsystems that may or may not be statically linked. Signed-off-by: Martin Rodriguez Reboredo --- v2 -> v3: - Generate bindings for USB. v1 -> v2: - Added this patch. drivers/usb/core/Kconfig | 7 +++++++ drivers/usb/core/Makefile | 3 +++ drivers/usb/core/usb.rs | 13 +++++++++++++ rust/bindings/bindings_helper.h | 1 + samples/rust/Kconfig | 10 ++++++++++ samples/rust/Makefile | 3 +++ samples/rust/rust_usb_simple.rs | 22 ++++++++++++++++++++++ 7 files changed, 59 insertions(+) create mode 100644 drivers/usb/core/usb.rs create mode 100644 samples/rust/rust_usb_simple.rs diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig index 351ede4b5de2..4b5604282129 100644 --- a/drivers/usb/core/Kconfig +++ b/drivers/usb/core/Kconfig @@ -116,3 +116,10 @@ config USB_AUTOSUSPEND_DELAY The default value Linux has always had is 2 seconds. Change this value if you want a different delay and cannot modify the command line or module parameter. + +config USB_RUST + bool "Rust USB bindings" + depends on USB && RUST + default n + help + Enables Rust bindings for USB. diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile index 7d338e9c0657..00e116913591 100644 --- a/drivers/usb/core/Makefile +++ b/drivers/usb/core/Makefile @@ -11,6 +11,7 @@ usbcore-y += phy.o port.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_PCI) += hcd-pci.o usbcore-$(CONFIG_ACPI) += usb-acpi.o +usbcore-$(CONFIG_USB_RUST) += libusb.rlib ifdef CONFIG_USB_ONBOARD_HUB usbcore-y += ../misc/onboard_usb_hub_pdevs.o @@ -18,4 +19,6 @@ endif obj-$(CONFIG_USB) += usbcore.o +rust-libs := ./usb + obj-$(CONFIG_USB_LEDS_TRIGGER_USBPORT) += ledtrig-usbport.o diff --git a/drivers/usb/core/usb.rs b/drivers/usb/core/usb.rs new file mode 100644 index 000000000000..3f7ad02153f5 --- /dev/null +++ b/drivers/usb/core/usb.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! USB devices and drivers. +//! +//! C header: [`include/linux/usb.h`](../../../../include/linux/usb.h) + +use kernel::bindings; + +/// Check if USB is disabled. +pub fn disabled() -> bool { + // SAFETY: FFI call. + unsafe { bindings::usb_disabled() != 0 } +} diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index c41eaab4ddb2..845cdd856981 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index b0f74a81c8f9..12116f6fb526 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT If unsure, say N. +config SAMPLE_RUST_USB_SIMPLE + tristate "USB simple device driver" + help + This option builds the Rust USB simple driver sample. + + To compile this as a module, choose M here: + the module will be called rust_usb_simple. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 03086dabbea4..f1ab58a9ecdd 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -2,5 +2,8 @@ obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o +obj-$(CONFIG_SAMPLE_RUST_USB_SIMPLE) += rust_usb_simple.o + +rust-libs := ../../drivers/usb/core/usb subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/rust_usb_simple.rs b/samples/rust/rust_usb_simple.rs new file mode 100644 index 000000000000..3523f81d5eb8 --- /dev/null +++ b/samples/rust/rust_usb_simple.rs @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust USB sample. + +use kernel::prelude::*; + +module! { + type: UsbSimple, + name: "rust_usb_simple", + author: "Martin Rodriguez Reboredo", + description: "Rust USB sample", + license: "GPL v2", +} + +struct UsbSimple; + +impl kernel::Module for UsbSimple { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("usb enabled: {}", !usb::disabled()); + Ok(UsbSimple) + } +}