From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by mx.groups.io with SMTP id smtpd.web09.6404.1660141136966524387 for ; Wed, 10 Aug 2022 07:18:57 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=H9LWA/xC; spf=pass (domain: gmail.com, ip: 209.85.215.169, mailfrom: ayushdevel1325@gmail.com) Received: by mail-pg1-f169.google.com with SMTP id c24so9624506pgg.11 for ; Wed, 10 Aug 2022 07:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc; bh=vP+Q8vm9gD7GgthUycwLdP2IBPMex1A3n3AKy3mJ5QU=; b=H9LWA/xCrbKAopKCn+9YiRgRTqQRbztZObUrmx66R6Qc1UUznbMtIBc4n77Wih2dyT jH9F7tF6y46B6pqn43nn6VF+VGLNLX9FirixciBD2UaQwkZN7p6uELvvRZpROfgbCiKi mFSqsRBlmacSKusnDeggwLIhBHvIu8Lwt6Fk3jEe85h/Dd6yO2mwC0njEVV22REJk9jJ TrtKegqII1caIhpwN77kLF021ai00Uk9/kJVlIbGSSyDdZQHXkjN27dxqWrkpMJirjp7 hReF8WohbZ4DSEyQYTRHRbvbxZVuGfr6rCNZjm1VRy4y5gMTl4rbqB4yXJV9zQSqYGD8 iUFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc; bh=vP+Q8vm9gD7GgthUycwLdP2IBPMex1A3n3AKy3mJ5QU=; b=DJAr5TX9sDrnXPGycsVss5VRrU2cF+6SWc/mzAlsl391aPFhc6dgsp4HyFRhYUGv8H 3I1FeroG7mvlV3EJ8+K1iFADQ+aVuty+PuhpUspwm7eueNGoVAhudPEe1w8XgWI+TzzF WdPNs/fLahEg9mQSL4BiCg27+EArRUMSKBXtx1Gr7Fsse5vCR2TUA3d8Gq8FD3YR1Axo 882cS7B2TPUTYgjZPMrcqIMaYv6FXX2BS5mL+7oYpTkUARIwiDN690HHU7Vxw3MZZueL 0Whfkf8RjpQaelAyjZ+ThCRdVsyuTPVGKc/T+jVGpPa3eRxJxaRiriolkp33J80pmizs CIyA== X-Gm-Message-State: ACgBeo0b33iLVpr1vyMCkX1DHpeSnWnxfAFyRjPdbMFejOUIZHpKymTu RGDcIOzU6xdSncaiEsFW9m8= X-Google-Smtp-Source: AA6agR7byJDW0rpgf11RIqaZQ4Z7V/PuXujZ90lCQfNtPo96bcwuMPY1nLjpRSHy343ssV5B45kZeA== X-Received: by 2002:a63:2110:0:b0:41d:234f:16aa with SMTP id h16-20020a632110000000b0041d234f16aamr17801942pgh.481.1660141136395; Wed, 10 Aug 2022 07:18:56 -0700 (PDT) Return-Path: Received: from [172.16.132.41] ([103.15.228.66]) by smtp.gmail.com with ESMTPSA id m186-20020a6326c3000000b0041d59062108sm6818998pgm.9.2022.08.10.07.18.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 Aug 2022 07:18:56 -0700 (PDT) Message-ID: <59f3b312-c3e0-7c13-e72d-a2a6049d8cff@gmail.com> Date: Wed, 10 Aug 2022 19:48:52 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [edk2-devel] Building C static Library for UEFI To: Andrew Fish , edk2-devel-groups-io Cc: Mike Kinney , "mikuback@linux.microsoft.com" , "Gaibusab, Jabeena B" , "Yao, Jiewen" References: <19101926-8aa0-8424-444b-495386572c74@gmail.com> <6F389C17-6413-44D7-B3EC-AE092026C118@apple.com> From: "Ayush Singh" In-Reply-To: <6F389C17-6413-44D7-B3EC-AE092026C118@apple.com> Content-Type: multipart/alternative; boundary="------------MJhmPOdsdjW3SKJKealmRCcY" Content-Language: en-US --------------MJhmPOdsdjW3SKJKealmRCcY Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Andrew. I did find out one solution that should work for Rust. The one I tried was number 3 [1]. Basically, you can use the native Windows target and it should be fine to link against. However, the C code I was trying to compile at that time (rust_test_helper), is not compatible with UEFI (uses some incompatible headers). I might write a version of `rust_test_helpers` for UEFI at some point, but it is not high priority right now. Ayush Singh [1]: https://doc.rust-lang.org/nightly/rustc/platform-support/unknown-uefi.html#cross-compilation-toolchains-and-c-code On 8/10/22 00:02, Andrew Fish wrote: > Did you figure this out? > > The per compiler flags come from BaseTools/Conf/tools_def.template. > When you do the edksetup.sh it gets copied to Conf/tools_def.ttxt and > that is what the build uses. The rules to generate makefiles come > from BaseTools/Conf/build_rule.template[2] and gets copied to > Conf/build_rule.txt > > You can override the generic compiler flags via a [BuildOptions] > section in an INF [3] of DSC [4]. > > [1] > https://github.com/tianocore/edk2/blob/master/BaseTools/Conf/tools_def.template > [2] > https://github.com/tianocore/edk2/blob/master/BaseTools/Conf/build_rule.template > [3] > https://github.com/tianocore/edk2/blob/master/UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf#L28 > > [4] > https://github.com/tianocore/edk2/blob/master/OvmfPkg/OvmfPkgX64.dsc#L111 > > > Thanks, > > Andrew Fish > > >> On Jul 30, 2022, at 7:25 AM, Ayush Singh >> wrote: >> >> Hello everyone. There are some tests in Rust std that statically link >> to a C static library to check ffi. Since if Rust is ever used in >> edk2, it would probably have to integrate with the existing C code. >> Thus, I thought rather than ignoring these tests, I wanted to make >> them work. >> >> What Rust cargo needs is a static C library (`.lib`) to link against. >> I was not really able to find much about linking in UEFI >> applications. However, from what I understand [1], edk2 already does >> do static link for C modules, so it should technically be the same >> for Rust as well as long as a static library can be generated from C >> code. I did look at the `build_rules` [2], but I don't know where the >> flags are defined. I think following rules: `C-Code-File` -> >> `Object-File` should technically create a static library, but would >> it work with projects outside edk2? Also, it would be great if >> someone can also help me with the flags that need to be supplied if >> it can. >> >> >> Yours Sincerely >> >> Ayush Singh >> >> >> [1]: >> https://edk2-docs.gitbook.io/edk-ii-build-specification/9_build_or_make_stage/95_dynamic_link >> >> [2]: >> https://github.com/tianocore/edk2/blob/e9150618ec91f79e70a1719ac8c198bee34a99be/BaseTools/Conf/build_rule.template >> >> > --------------MJhmPOdsdjW3SKJKealmRCcY Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi Andrew. I did find out one solution that should work for Rust. The one I tried was number 3 [1]. Basically, you can use the native Windows target and it should be fine to link against. However, the C code I was trying to compile at that time (rust_test_helper), is not compatible with UEFI (uses some incompatible headers). I might write a version of `rust_test_helpers` for UEFI at some point, but it is not high priority right now.


Ayush Singh

[1]: https://doc.rust-lang.org/nightly/rustc/platform-support/unknown-uefi.html#cross-compilation-toolchains-and-c-code


On 8/10/22 00:02, Andrew Fish wrote:
Did you figure this out?

The per compiler flags come from BaseTools/Conf/tools_def.template. When you do the edksetup.sh it gets copied to Conf/tools_def.ttxt and that is what the build uses. The rules to generate makefiles come from BaseTools/Conf/build_rule.template[2] and gets copied to Conf/build_rule.txt

You can override the generic compiler flags via a [BuildOptions] section in an INF [3] of DSC [4]. 

[2] https://github.com/tianocore/edk2/blob/master/BaseTools/Conf/build_rule.template


Thanks,

Andrew Fish


On Jul 30, 2022, at 7:25 AM, Ayush Singh <ayushdevel1325@gmail.com> wrote:

Hello everyone. There are some tests in Rust std that statically link to a C static library to check ffi. Since if Rust is ever used in edk2, it would probably have to integrate with the existing C code. Thus, I thought rather than ignoring these tests, I wanted to make them work.

What Rust cargo needs is a static C library (`.lib`) to link against. I was not really able to find much about linking in UEFI applications. However, from what I understand [1], edk2 already does do static link for C modules, so it should technically be the same for Rust as well as long as a static library can be generated from C code. I did look at the `build_rules` [2], but I don't know where the flags are defined. I think following rules: `C-Code-File` -> `Object-File` should technically create a static library, but would it work with projects outside edk2? Also, it would be great if someone can also help me with the flags that need to be supplied if it can.


Yours Sincerely

Ayush Singh


[1]: https://edk2-docs.gitbook.io/edk-ii-build-specification/9_build_or_make_stage/95_dynamic_link

[2]: https://github.com/tianocore/edk2/blob/e9150618ec91f79e70a1719ac8c198bee34a99be/BaseTools/Conf/build_rule.template


--------------MJhmPOdsdjW3SKJKealmRCcY--