From 781d64cbcfc18effc2b4786d41cc2fbcaa5dc0d0 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 08:46:37 -0700 Subject: [PATCH 01/11] initial commit --- bootstrap.sh | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ test_lib.sh | 24 +++++++++ 2 files changed, 162 insertions(+) create mode 100755 bootstrap.sh create mode 100755 test_lib.sh diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..20846ea --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,138 @@ +set -eu + +LLVM_URL=https://github.com/llvm/llvm-project.git +LINUX_URL=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git +MUSL_URL=git://git.musl-libc.org/musl + +function update_llvm () { + pushd llvm-project + git fetch --depth 1 + git reset --hard origin/main + popd +} + +function fetch_llvm () { + git clone --depth=1 $LLVM_URL --branch main --single-branch +} + +function get_or_fetch_llvm () { + if [[ -d llvm-project ]]; then + update_llvm + else + fetch_llvm + fi; +} + +function update_linux () { + pushd linux + git fetch --depth 1 + git reset --hard origin/master + popd +} + +function fetch_linux () { + git clone --depth=1 $LINUX_URL --branch master --single-branch +} + +function get_or_fetch_linux_kernel () { + if [[ -d linux ]]; then + update_linux + else + fetch_linux + fi; +} + +function build_kernel_headers () { + if [[ -d kernel-headers ]]; then + return + fi + + pushd linux + make LLVM=1 INSTALL_HDR_PATH=../kernel-headers mrproper headers_install -j$(nproc) + popd +} + +function bootstrap_compiler_rt () { + rm -rf sysroot + mkdir -p sysroot + SYSROOT=$(readlink -f sysroot) + CC=$(which clang) + CXX=$(which clang++) + + #rm -rf llvm-project/compiler-rt/build + mkdir -p llvm-project/compiler-rt/build + pushd llvm-project/compiler-rt/build + cmake -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_CXX_COMPILER=$CXX \ + -D CMAKE_CXX_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D CMAKE_C_COMPILER=$CC \ + -D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D COMPILER_RT_BUILD_LIBFUZZER=NO \ + -D COMPILER_RT_BUILD_MEMPROF=NO \ + -D COMPILER_RT_BUILD_ORC=NO \ + -D COMPILER_RT_BUILD_PROFILE=NO \ + -D COMPILER_RT_BUILD_SANITIZERS=NO \ + -D COMPILER_RT_BUILD_XRAY=NO \ + -D COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl \ + -D LLVM_ENABLE_PROJECTS="compiler-rt;" \ + -D LLVM_TARGETS_TO_BUILD="X86;" \ + -G Ninja \ + -S .. + #-D CMAKE_INSTALL_LIBDIR=lib \ + #-D CMAKE_INSTALL_PREFIX=woof \ + #-D COMPILER_RT_INSTALL_PATH=woof \ + #-D COMPILER_RT_INSTALL_LIBRARY_DIR=woof \ + ninja compiler-rt + DESTDIR=$SYSROOT ninja install + popd +} + +function update_musl () { + pushd musl + git fetch --depth 1 + git reset --hard origin/master + popd +} + +function fetch_musl () { + git clone --depth=1 $MUSL_URL --branch master --single-branch +} + +function get_or_fetch_musl () { + if [[ -d musl ]]; then + update_musl + else + fetch_musl + fi; +} + +function build_musl () { + BUILTINS=$(readlink -f sysroot/usr/local/lib/linux/libclang_rt.builtins-x86_64.a) + CC=$(which clang) + SYSROOT=$(readlink -f sysroot) + rm -rf musl/build + mkdir -p musl/build + pushd musl/build + LIBCC=$BUILTINS CC=$CC ../configure \ + --prefix=/usr/local/ \ + --host=x86_64-unknown-linux-musl \ + --syslibdir=/usr/local/lib \ + --disable-static + + make -j$(nproc) AR=llvm-ar RANLIB=llvm-ranlib + make DESTDIR=$SYSROOT install-libs install-headers -j$(nproc) + + # TODO: hack + pushd $SYSROOT/usr/local/lib + ln -sf libc.so ld-musl-x86_64.so.1 + popd + popd + ./test_lib.sh +} + +#get_or_fetch_llvm +bootstrap_compiler_rt +#get_or_fetch_linux_kernel +build_kernel_headers +#get_or_fetch_musl +build_musl diff --git a/test_lib.sh b/test_lib.sh new file mode 100755 index 0000000..9a1e1ef --- /dev/null +++ b/test_lib.sh @@ -0,0 +1,24 @@ +set -eu + +cat << EOF > test.c +#include +int main() { puts("hello world"); } +EOF + +# -fuse-ld: use LLD rather than BFD. +# -rtlib: use compiler-rt rather than libgcc. +# -Xlinker --dynamic-linker: use musl's dynamic loader as the program +# interperter. +# --target: use musl rather than glibc. +# -resource-dir: use our compiler-rt builtins. +# --sysroot: use our libc headers +clang test.c \ + -fuse-ld=lld \ + -rtlib=compiler-rt \ + -Xlinker --dynamic-linker=sysroot/usr/local/lib/ld-musl-x86_64.so.1 \ + --target=x86_64-unknown-linux-musl \ + -resource-dir=sysroot/usr/local \ + --sysroot=sysroot/usr/local + +./a.out +rm -f test.c a.out From e8e6cd9f004d58a381ee8c271b450e6bf43fd664 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 08:55:40 -0700 Subject: [PATCH 02/11] move kernel header to separate script --- bootstrap.sh | 33 +-------------------------------- kernel.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) create mode 100755 kernel.sh diff --git a/bootstrap.sh b/bootstrap.sh index 20846ea..2a9dd62 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,7 +1,6 @@ set -eu LLVM_URL=https://github.com/llvm/llvm-project.git -LINUX_URL=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git MUSL_URL=git://git.musl-libc.org/musl function update_llvm () { @@ -23,35 +22,6 @@ function get_or_fetch_llvm () { fi; } -function update_linux () { - pushd linux - git fetch --depth 1 - git reset --hard origin/master - popd -} - -function fetch_linux () { - git clone --depth=1 $LINUX_URL --branch master --single-branch -} - -function get_or_fetch_linux_kernel () { - if [[ -d linux ]]; then - update_linux - else - fetch_linux - fi; -} - -function build_kernel_headers () { - if [[ -d kernel-headers ]]; then - return - fi - - pushd linux - make LLVM=1 INSTALL_HDR_PATH=../kernel-headers mrproper headers_install -j$(nproc) - popd -} - function bootstrap_compiler_rt () { rm -rf sysroot mkdir -p sysroot @@ -132,7 +102,6 @@ function build_musl () { #get_or_fetch_llvm bootstrap_compiler_rt -#get_or_fetch_linux_kernel -build_kernel_headers +./kernel.sh #get_or_fetch_musl build_musl diff --git a/kernel.sh b/kernel.sh new file mode 100755 index 0000000..49c46c4 --- /dev/null +++ b/kernel.sh @@ -0,0 +1,35 @@ +set -eu + +LINUX_URL=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git + +function update_linux () { + pushd linux + git fetch --depth 1 + git reset --hard origin/master + popd +} + +function fetch_linux () { + git clone --depth=1 $LINUX_URL --branch master --single-branch +} + +function get_or_fetch_linux_kernel () { + if [[ -d linux ]]; then + update_linux + else + fetch_linux + fi; +} + +function build_kernel_headers () { + if [[ -d kernel-headers ]]; then + return + fi + + pushd linux + make LLVM=1 INSTALL_HDR_PATH=../kernel-headers mrproper headers_install -j$(nproc) + popd +} + +get_or_fetch_linux_kernel +build_kernel_headers From c97a2510eaaaf17ead0a92921913313dc8d484bf Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 09:23:40 -0700 Subject: [PATCH 03/11] move musl code to separate script --- bootstrap.sh | 51 +++-------------------------------------------- musl.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 48 deletions(-) create mode 100755 musl.sh diff --git a/bootstrap.sh b/bootstrap.sh index 2a9dd62..c08cc2b 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,7 +1,6 @@ set -eu LLVM_URL=https://github.com/llvm/llvm-project.git -MUSL_URL=git://git.musl-libc.org/musl function update_llvm () { pushd llvm-project @@ -23,7 +22,7 @@ function get_or_fetch_llvm () { } function bootstrap_compiler_rt () { - rm -rf sysroot + #rm -rf sysroot mkdir -p sysroot SYSROOT=$(readlink -f sysroot) CC=$(which clang) @@ -57,51 +56,7 @@ function bootstrap_compiler_rt () { popd } -function update_musl () { - pushd musl - git fetch --depth 1 - git reset --hard origin/master - popd -} - -function fetch_musl () { - git clone --depth=1 $MUSL_URL --branch master --single-branch -} - -function get_or_fetch_musl () { - if [[ -d musl ]]; then - update_musl - else - fetch_musl - fi; -} - -function build_musl () { - BUILTINS=$(readlink -f sysroot/usr/local/lib/linux/libclang_rt.builtins-x86_64.a) - CC=$(which clang) - SYSROOT=$(readlink -f sysroot) - rm -rf musl/build - mkdir -p musl/build - pushd musl/build - LIBCC=$BUILTINS CC=$CC ../configure \ - --prefix=/usr/local/ \ - --host=x86_64-unknown-linux-musl \ - --syslibdir=/usr/local/lib \ - --disable-static - - make -j$(nproc) AR=llvm-ar RANLIB=llvm-ranlib - make DESTDIR=$SYSROOT install-libs install-headers -j$(nproc) - - # TODO: hack - pushd $SYSROOT/usr/local/lib - ln -sf libc.so ld-musl-x86_64.so.1 - popd - popd - ./test_lib.sh -} - -#get_or_fetch_llvm +get_or_fetch_llvm bootstrap_compiler_rt ./kernel.sh -#get_or_fetch_musl -build_musl +./musl.sh diff --git a/musl.sh b/musl.sh new file mode 100755 index 0000000..3eb9465 --- /dev/null +++ b/musl.sh @@ -0,0 +1,56 @@ +set -eu + +MUSL_URL=git://git.musl-libc.org/musl + +function update_musl () { + pushd musl + git fetch --depth 1 + git reset --hard origin/master + popd +} + +function fetch_musl () { + git clone --depth=1 $MUSL_URL --branch master --single-branch +} + +function get_or_fetch_musl () { + if [[ -d musl ]]; then + update_musl + else + fetch_musl + fi; +} + +function build_musl () { + # TODO: something better than this...like if we updated musl. + if [[ -d sysroot/usr/local/include ]]; then + if [[ -n "$(find sysroot/usr/local/include -type f | wc -l)" ]]; then + return + fi; + fi; + + BUILTINS=$(readlink -f sysroot/usr/local/lib/linux/libclang_rt.builtins-x86_64.a) + CC=$(which clang) + SYSROOT=$(readlink -f sysroot) + rm -rf musl/build + mkdir -p musl/build + pushd musl/build + LIBCC=$BUILTINS CC=$CC ../configure \ + --prefix=/usr/local/ \ + --host=x86_64-unknown-linux-musl \ + --syslibdir=/usr/local/lib \ + --disable-static + + make -j$(nproc) AR=llvm-ar RANLIB=llvm-ranlib + make DESTDIR=$SYSROOT install-libs install-headers -j$(nproc) + + # TODO: hack + pushd $SYSROOT/usr/local/lib + ln -sf libc.so ld-musl-x86_64.so.1 + popd + popd + ./test_lib.sh +} + +get_or_fetch_musl +build_musl From c5c907e0caf41d44333f2df84ff176281a948d75 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 09:40:52 -0700 Subject: [PATCH 04/11] build libunwind --- bootstrap.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bootstrap.sh b/bootstrap.sh index c08cc2b..55025fb 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -56,7 +56,34 @@ function bootstrap_compiler_rt () { popd } +function build_libunwind () { + SYSROOT=$(readlink -f sysroot) + RESOURCE=$(readlink -f sysroot/usr/local) + CC=$(which clang) + CXX=$(which clang++) + + mkdir -p llvm-project/libunwind/build + pushd llvm-project/libunwind/build + cmake -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_CXX_COMPILER=$CXX \ + -D CMAKE_CXX_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D CMAKE_C_COMPILER=$CC \ + -D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D CMAKE_SHARED_LINKER_FLAGS=-resource-dir=$RESOURCE \ + -D LIBUNWIND_ENABLE_STATIC=NO \ + -D LIBUNWIND_USE_COMPILER_RT=YES \ + -D LIBUNWIND_INCLUDE_DOCS=NO \ + -D LLVM_ENABLE_PROJECTS="libunwind" \ + -D LLVM_TARGETS_TO_BUILD="X86;" \ + -G Ninja \ + -S .. + ninja libunwind.so + DESTDIR=$SYSROOT ninja install + popd +} + get_or_fetch_llvm bootstrap_compiler_rt ./kernel.sh ./musl.sh +build_libunwind From f6bcf368b9dac685299cb4ad67494714c98e2390 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 11:37:28 -0700 Subject: [PATCH 05/11] libcxxabi --- bootstrap.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 55025fb..1de5c44 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -4,8 +4,10 @@ LLVM_URL=https://github.com/llvm/llvm-project.git function update_llvm () { pushd llvm-project - git fetch --depth 1 - git reset --hard origin/main + # hack for https://reviews.llvm.org/D97572. + git fetch --depth 1 origin 3a6365a439ede4b7c65076bb42b1b7dbf72216b5 + #git fetch --depth 1 + git reset --hard FETCH_HEAD popd } @@ -51,6 +53,7 @@ function bootstrap_compiler_rt () { #-D CMAKE_INSTALL_PREFIX=woof \ #-D COMPILER_RT_INSTALL_PATH=woof \ #-D COMPILER_RT_INSTALL_LIBRARY_DIR=woof \ + #-D COMPILER_RT_BUILD_CRT=YES \ ninja compiler-rt DESTDIR=$SYSROOT ninja install popd @@ -82,8 +85,58 @@ function build_libunwind () { popd } -get_or_fetch_llvm -bootstrap_compiler_rt -./kernel.sh -./musl.sh -build_libunwind +function build_libcxxabi () { + SYSROOT=$(readlink -f sysroot/) + RESOURCE=$(readlink -f sysroot/usr/local) + LIBCXX=$(readlink -f llvm-project/libcxx) + CC=$(which clang) + CXX=$(which clang++) + + # TODO: a hack + pushd sysroot/usr/local/lib + if [[ ! -e crtbeginS.o ]]; then + ln -s linux/clang_rt.crtbegin-x86_64.o crtbeginS.o + fi + if [[ ! -e crtendS.o ]]; then + ln -s linux/clang_rt.crtend-x86_64.o crtendS.o + fi + popd + + rm -rf llvm-project/libcxxabi/build # + mkdir -p llvm-project/libcxxabi/build + pushd llvm-project/libcxxabi/build + # TODO: link with lld + cmake -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_CXX_COMPILER_WORKS=YES \ + -D CMAKE_CXX_COMPILER=$CXX \ + -D CMAKE_CXX_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D CMAKE_C_COMPILER=$CC \ + -D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D LIBCXXABI_ENABLE_STATIC=NO \ + -D LIBCXXABI_LIBCXX_INCLUDES=$LIBCXX/include \ + -D LIBCXXABI_LIBCXX_PATH=$LIBCXX \ + -D LIBCXXABI_TARGET_TRIPLE=x86_64-unknown-linux-musl \ + -D LIBCXXABI_USE_COMPILER_RT=YES \ + -D LIBCXXABI_USE_LLVM_UNWINDER=YES \ + -D LIBCXXABI_SYSROOT=$RESOURCE \ + -D LIBCXXABI_SUPPORTS_NOSTDLIBXX_FLAG=NO \ + -D CMAKE_EXE_LINKER_FLAGS="-rtlib=compiler-rt -resource-dir=$RESOURCE --sysroot=$RESOURCE" \ + -G Ninja \ + -S .. + #-D CMAKE_SHARED_LINKER_FLAGS=-resource-dir=$RESOURCE \ + #-D LLVM_ENABLE_PROJECTS="libcxxabi" \ + #-D LLVM_TARGETS_TO_BUILD="X86;" \ + # -D CMAKE_CXX_COMPILER_WORKS=YES seems like a hack, but we can't test + # what we haven't built yet. + #--debug-trycompile \ + ninja libc++abi.so + DESTDIR=$SYSROOT ninja install + popd +} + +#get_or_fetch_llvm +#bootstrap_compiler_rt +#./kernel.sh +#./musl.sh +#build_libunwind +build_libcxxabi From 2a4f56269e6d776215d6a76707a29b8946b04b8a Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 13:25:18 -0700 Subject: [PATCH 06/11] license+readme --- LICENSE-2.0.txt | 202 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 29 +++++++ 2 files changed, 231 insertions(+) create mode 100644 LICENSE-2.0.txt create mode 100644 README.md diff --git a/LICENSE-2.0.txt b/LICENSE-2.0.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE-2.0.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e42cace --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +A set of (poorly-written) shell scripts to demonstrate building LLVM against +musl. + +This code is for demonstration purposes only; it should be used only as +reference for cleaner build systems. + +An explicit goal is to try to keep the CMAKE variables as explicit in the +sources as possible. + +This work is heavily derived and derived entirely from Saleem Abdulrasool's +(@compnerd) +[gist](https://gist.github.com/compnerd/ebbc625a359d1d3e292e1fd2007ecb52). + +### License +``` +Copyright 2021 The ClangBuiltLinux project contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` From 9d50aa1142c0a3b61d212d659e596e41e4053288 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 14:00:14 -0700 Subject: [PATCH 07/11] document picture of sysroot --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index e42cace..aad5132 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,42 @@ This work is heavily derived and derived entirely from Saleem Abdulrasool's (@compnerd) [gist](https://gist.github.com/compnerd/ebbc625a359d1d3e292e1fd2007ecb52). +### Sysroot + +Builds a sysroot that looks like: +``` +sysroot +└── usr + └── local + ├── include + │   ├── ... + └── lib + ├── crt1.o + ├── crtbeginS.o -> linux/clang_rt.crtbegin-x86_64.o + ├── crtendS.o -> linux/clang_rt.crtend-x86_64.o + ├── crti.o + ├── crtn.o + ├── ld-musl-x86_64.so.1 -> libc.so + ├── libc++abi.so -> libc++abi.so.1 + ├── libc++abi.so.1 -> libc++abi.so.1.0 + ├── libc++abi.so.1.0 + ├── libcrypt.a + ├── libc.so + ├── libdl.a + ├── libm.a + ├── libpthread.a + ├── libresolv.a + ├── librt.a + ├── libunwind.so -> libunwind.so.1 + ├── libunwind.so.1 -> libunwind.so.1.0 + ├── libunwind.so.1.0 + ├── libutil.a + ├── libxnet.a + ├── linux + ├── rcrt1.o + └── Scrt1.o +``` + ### License ``` Copyright 2021 The ClangBuiltLinux project contributors From 5c95e4806026076ff1cc6a2794a99001734767c5 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 13:56:16 -0700 Subject: [PATCH 08/11] WIP: trying to get libc++ to build also, I had libc++abi built, but upon wiping my build artifacts and rerunning fresh, that doesn't build anymore... --- .gitignore | 4 +++- bootstrap.sh | 68 +++++++++++++++++++++++++++++++++++++++++++++------- kernel.sh | 6 ++--- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 8e96f8f..6cb4c33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ +# third party sources + linux/ llvm-project/ musl/ +# build outputs sysroot/ -kernel-headers/ diff --git a/bootstrap.sh b/bootstrap.sh index 1de5c44..950c63e 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -6,13 +6,21 @@ function update_llvm () { pushd llvm-project # hack for https://reviews.llvm.org/D97572. git fetch --depth 1 origin 3a6365a439ede4b7c65076bb42b1b7dbf72216b5 + #git checkout FETCH_HEAD #git fetch --depth 1 git reset --hard FETCH_HEAD popd } function fetch_llvm () { - git clone --depth=1 $LLVM_URL --branch main --single-branch + #git clone --depth=1 $LLVM_URL --branch main --single-branch + mkdir llvm-project + pushd llvm-project + git init + git remote add origin $LLVM_URL + git fetch --depth 1 origin 3a6365a439ede4b7c65076bb42b1b7dbf72216b5 + git checkout FETCH_HEAD + popd } function get_or_fetch_llvm () { @@ -85,6 +93,8 @@ function build_libunwind () { popd } +# Saleem: c++abi is the libc equivalent for c++: it is the language runtime; +# libc++ is the C++ standard library function build_libcxxabi () { SYSROOT=$(readlink -f sysroot/) RESOURCE=$(readlink -f sysroot/usr/local) @@ -102,7 +112,7 @@ function build_libcxxabi () { fi popd - rm -rf llvm-project/libcxxabi/build # + #rm -rf llvm-project/libcxxabi/build # mkdir -p llvm-project/libcxxabi/build pushd llvm-project/libcxxabi/build # TODO: link with lld @@ -134,9 +144,51 @@ function build_libcxxabi () { popd } -#get_or_fetch_llvm -#bootstrap_compiler_rt -#./kernel.sh -#./musl.sh -#build_libunwind -build_libcxxabi +function build_libcxx () { + CC=$(which clang) + CXX=$(which clang++) + SYSROOT=$(readlink -f sysroot/) + RESOURCE=$(readlink -f sysroot/usr/local) + + #rm -rf llvm-project/libcxx/build # + mkdir -p llvm-project/libcxx/build + pushd llvm-project/libcxx/build + + # TODO: link with lld + cmake \ + -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_CXX_COMPILER=$CXX \ + -D CMAKE_CXX_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D CMAKE_CXX_COMPILER_WORKS=YES \ + -D CMAKE_C_COMPILER=$CC \ + -D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \ + -D LIBCXX_ENABLE_STATIC=NO \ + -D LIBCXX_CXX_ABI=libcxxabi \ + -D LIBCXX_TARGET_TRIPLE=x86_64-unknown-linux-musl \ + -D LIBCXX_SYSROOT=$SYSROOT/usr/local \ + -D LIBCXX_HAS_MUSL_LIBC=YES \ + -D LIBCXX_USE_COMPILER_RT=YES \ + --debug-trycompile \ + -D CMAKE_EXE_LINKER_FLAGS="-rtlib=compiler-rt -resource-dir=$RESOURCE --sysroot=$RESOURCE" \ + -G Ninja \ + -S .. + #-D CMAKE_C_COMPILER_WORKS=YES \ + #-D LIBCXX_LINK_FLAGS="-stdlib=libc++" \ + #-D LIBCXX_LINK_FLAGS="-nodefaultlibs -lc -rtlib=compiler-rt" \ + ninja libc++.so + + + popd +} + +get_or_fetch_llvm +bootstrap_compiler_rt +./kernel.sh +./musl.sh +build_libunwind +#build_libcxxabi +#TODO: this doesn't work yet +#build_libcxx + +# Fangrui says try: +# -DLLVM_HOST_TRIPLE= diff --git a/kernel.sh b/kernel.sh index 49c46c4..c3111cf 100755 --- a/kernel.sh +++ b/kernel.sh @@ -22,12 +22,10 @@ function get_or_fetch_linux_kernel () { } function build_kernel_headers () { - if [[ -d kernel-headers ]]; then - return - fi + SYSROOT=$(readlink -f sysroot) pushd linux - make LLVM=1 INSTALL_HDR_PATH=../kernel-headers mrproper headers_install -j$(nproc) + make LLVM=1 INSTALL_HDR_PATH=$SYSROOT/usr/local mrproper headers_install -j$(nproc) popd } From 6a08c944a60d39621d4cb6de55061318a6f9d8b6 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 14:14:42 -0700 Subject: [PATCH 09/11] remove bad check that was preventing musl headers from being installed, breaking the build of libcxxabi. --- bootstrap.sh | 2 +- musl.sh | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 950c63e..b2c69ab 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -186,7 +186,7 @@ bootstrap_compiler_rt ./kernel.sh ./musl.sh build_libunwind -#build_libcxxabi +build_libcxxabi #TODO: this doesn't work yet #build_libcxx diff --git a/musl.sh b/musl.sh index 3eb9465..55f06a9 100755 --- a/musl.sh +++ b/musl.sh @@ -23,11 +23,11 @@ function get_or_fetch_musl () { function build_musl () { # TODO: something better than this...like if we updated musl. - if [[ -d sysroot/usr/local/include ]]; then - if [[ -n "$(find sysroot/usr/local/include -type f | wc -l)" ]]; then - return - fi; - fi; + #if [[ -d sysroot/usr/local/include ]]; then + #if [[ -n "$(find sysroot/usr/local/include -type f | wc -l)" ]]; then + #return + #fi; + #fi; BUILTINS=$(readlink -f sysroot/usr/local/lib/linux/libclang_rt.builtins-x86_64.a) CC=$(which clang) From 9c7274159b1d09f885f5916ece93c7402f58deab Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 15:41:19 -0700 Subject: [PATCH 10/11] get libc++ building thanks to @compnerd --- README.md | 5 +++++ bootstrap.sh | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aad5132..3693a56 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ sysroot ├── libc++abi.so.1.0 ├── libcrypt.a ├── libc.so + ├── libc++.so.1 -> libc++.so.1.0 + ├── libc++.so.1.0 ├── libdl.a ├── libm.a ├── libpthread.a @@ -43,6 +45,9 @@ sysroot ├── libutil.a ├── libxnet.a ├── linux + │   ├── clang_rt.crtbegin-x86_64.o + │   ├── clang_rt.crtend-x86_64.o + │   └── libclang_rt.builtins-x86_64.a ├── rcrt1.o └── Scrt1.o ``` diff --git a/bootstrap.sh b/bootstrap.sh index b2c69ab..b43154d 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -168,15 +168,19 @@ function build_libcxx () { -D LIBCXX_SYSROOT=$SYSROOT/usr/local \ -D LIBCXX_HAS_MUSL_LIBC=YES \ -D LIBCXX_USE_COMPILER_RT=YES \ - --debug-trycompile \ + -D LIBCXX_INCLUDE_BENCHMARKS=NO \ -D CMAKE_EXE_LINKER_FLAGS="-rtlib=compiler-rt -resource-dir=$RESOURCE --sysroot=$RESOURCE" \ + -D LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG=NO \ -G Ninja \ -S .. + # TODO: should we add LIBCXX_BUILTINS_LIBRARY="${SYSROOT}/usr/local/lib/linux/libclang_rt.builtins-x86_64.a" + # as per @compnerd? + #--debug-trycompile \ #-D CMAKE_C_COMPILER_WORKS=YES \ #-D LIBCXX_LINK_FLAGS="-stdlib=libc++" \ #-D LIBCXX_LINK_FLAGS="-nodefaultlibs -lc -rtlib=compiler-rt" \ ninja libc++.so - + DESTDIR=$SYSROOT ninja install-cxx install-cxx-headers popd } @@ -187,8 +191,7 @@ bootstrap_compiler_rt ./musl.sh build_libunwind build_libcxxabi -#TODO: this doesn't work yet -#build_libcxx +build_libcxx # Fangrui says try: # -DLLVM_HOST_TRIPLE= From 2b697bd6c56f11cb35081377b3db5f6cc7eca927 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Wed, 22 Sep 2021 17:48:28 -0700 Subject: [PATCH 11/11] fixup libunwind --- bootstrap.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index b43154d..d68de85 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -73,6 +73,10 @@ function build_libunwind () { CC=$(which clang) CXX=$(which clang++) + # - LIBUNWIND_COMPILE_FLAGS: flags used to compile libunwind + # - LIBUNWIND_LINK_FLAGS: flags used to link libunwind + # - LIBUNWIND_LIBRARIES: libraries to link libunwind to. + rm -rf llvm-project/libunwind/build # mkdir -p llvm-project/libunwind/build pushd llvm-project/libunwind/build cmake -D CMAKE_BUILD_TYPE=Release \ @@ -82,12 +86,24 @@ function build_libunwind () { -D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \ -D CMAKE_SHARED_LINKER_FLAGS=-resource-dir=$RESOURCE \ -D LIBUNWIND_ENABLE_STATIC=NO \ - -D LIBUNWIND_USE_COMPILER_RT=YES \ -D LIBUNWIND_INCLUDE_DOCS=NO \ - -D LLVM_ENABLE_PROJECTS="libunwind" \ + -D LIBUNWIND_USE_COMPILER_RT=YES \ -D LLVM_TARGETS_TO_BUILD="X86;" \ + -D LIBUNWIND_TARGET_TRIPLE=x86_64-unknown-linux-musl \ + -D LIBUNWIND_SYSROOT=$RESOURCE \ + -D CMAKE_REQUIRED_FLAGS="-rtlib=compiler-rt -resource-dir=$RESOURCE" \ + --debug-trycompile \ -G Ninja \ -S .. + #-D CMAKE_REQUIRED_LINK_OPTIONS="-rtlib=compiler-rt;-resource-dir=$RESOURCE" \ + #-D CMAKE_EXE_LINKER_FLAGS="-Xlinker --dynamic-linker=$RESOURCE/lib/ld-musl-x86_64.so.1" \ + #-D LIBUNWIND_COMPILE_FLAGS="-Xlinker --dynamic-linker=$RESOURCE/lib/ld-musl-x86_64.so.1" \ + #-D LIBUNWIND_LINK_FLAGS="-Xlinker --dynamic-linker=$RESOURCE/lib/ld-musl-x86_64.so.1" \ + #-D LIBUNWIND_LINK_FLAGS="--sysroot=$RESOURCE" \ + #-D CMAKE_CXX_COMPILER_WORKS=YES \ + #-D CMAKE_EXE_LINKER_FLAGS="-nostdlib" \ + #-D LLVM_ENABLE_PROJECTS="libunwind" \ + #-D CMAKE_EXE_LINKER_FLAGS="-resource-dir=$RESOURCE --sysroot=$RESOURCE" \ ninja libunwind.so DESTDIR=$SYSROOT ninja install popd