From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 63E573858406; Fri, 15 Jul 2022 21:38:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 63E573858406 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Merge #1362 X-Act-Checkin: gcc X-Git-Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com> X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 67f9b173b9061ceef0fd96413578b0e5ddcac061 X-Git-Newrev: fd2bd659e44e5b7fea92bc34a4864f057f387490 Message-Id: <20220715213822.63E573858406@sourceware.org> Date: Fri, 15 Jul 2022 21:38:22 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jul 2022 21:38:22 -0000 https://gcc.gnu.org/g:fd2bd659e44e5b7fea92bc34a4864f057f387490 commit fd2bd659e44e5b7fea92bc34a4864f057f387490 Merge: 67f9b173b90 2d1378f7310 Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com> Date: Fri Jul 15 16:09:43 2022 +0000 Merge #1362 1362: extern crate loading r=philberty a=philberty WIP still trying to write dejagnu to enable automated tests Extern crates statements to tell the front-end to look for another library. The mechanism here is heavily inspired from gccgo, so when we compile a library for example we invoke: ``` gccrs -g -O2 -frust-crate=mylib -c src/lib.rs -o src/mylib.o ``` All going well this object file will now contain extra data inside .rust-export section inside the object file which will be preserved inside archives and shared objects. When we have another application which uses this library 'mylib'. ```rust extern crate mylib; use mylib::foo; fn main() { foo(); } ``` We compile using: ``` gcc -g -O2 -frust-crate=test -c src/main.rs -o src/main.o ``` When the extern crate line is hit the front-end will look for mylib.o, libmylib.a, mylib.rox. If it finds a raw object file it will read the .rust-export section directly from the object for the public metadata such as public functions, types constants etc. If it fails to find an object it might find .rox which is the objdump of the .rust-export to a raw file, it might even find libmylib.a and read the export directly out of the archive file reusing code from gccgo to do so. The full compiler pipeline is reused here, so the metatadata is actually just real rust code. The benifit here is that Rust supports exporting, macros and generics so this requires the name-resolution and type info all to be generated and inserted into the apropriate context classes. Since the metadata is real rust code it means we can reuse the full pipeline to generate the code as nessecary. So for the simple case of a public struct we simply emit the AST dump of this struct directly into the metadata. If its a non-generic public function we emit and extern rust abi block for that function. If its a trait we can simply emit the trait with the public memebers. Generics are more complicated since we need to emit the function fully for it to be compiled correctly this still needs tests to be added. The hardest part is non generic impl blocks which is still a WIP. To finally link the two crates together you run: ``` gcc -g -O2 -o rust-program.exe src/main.o src/mylib.o ``` Fixes: 1169 Co-authored-by: Philip Herron Diff: gcc/rust/Make-lang.in | 12 +- gcc/rust/ast/rust-ast-dump.cc | 69 +- gcc/rust/ast/rust-ast-dump.h | 1 + gcc/rust/expand/rust-attribute-visitor.cc | 17 +- gcc/rust/lang.opt | 8 + gcc/rust/metadata/rust-export-metadata.cc | 385 +++++++++++ gcc/rust/metadata/rust-export-metadata.h | 85 +++ gcc/rust/metadata/rust-extern-crate.cc | 173 +++++ gcc/rust/metadata/rust-extern-crate.h | 55 ++ gcc/rust/metadata/rust-import-archive.cc | 885 ++++++++++++++++++++++++++ gcc/rust/metadata/rust-imports.cc | 441 +++++++++++++ gcc/rust/metadata/rust-imports.h | 257 ++++++++ gcc/rust/resolve/rust-ast-resolve-toplevel.h | 60 +- gcc/rust/rust-object-export.cc | 6 - gcc/rust/rust-session-manager.cc | 110 +++- gcc/rust/rust-session-manager.h | 19 +- gcc/testsuite/rust/link/generic_function_0.rs | 7 + gcc/testsuite/rust/link/generic_function_1.rs | 3 + gcc/testsuite/rust/link/link.exp | 163 +++++ gcc/testsuite/rust/link/simple_function_0.rs | 7 + gcc/testsuite/rust/link/simple_function_1.rs | 3 + gcc/testsuite/rust/link/trait_import_0.rs | 19 + gcc/testsuite/rust/link/trait_import_1.rs | 6 + 23 files changed, 2740 insertions(+), 51 deletions(-)