Skip to content

Commit f3d451e

Browse files
authored
Script changes for building static SDK with swift-foundation (#403)
* Fix Job Count Flag Hooking up the job count flag to set the number of parallel jobs and if there is an unknown flag, emit an error message saying that the flag is instead of just printing out the usage info. * Skip cmake bootstrap The cmake bootstrap is slower than using the installed CMake to build the new CMake since it has to build a limited CMake first and then use that for the full CMake. This uses the existing CMake 3.22 from the container to build the CMake from the Swift checkout with SSL disabled, testing disabled, and the interactive curses UI disabled to improve the overall build times. * Fixing flags for static SDK build Updating the C++ compiler flags. - `-stdlib=libc++`: modifies the stdlib header search paths, so that needs to be on the cxx call. - `-unwindlib=libunwind`: should only be needed on the link job, but one of the dependencies was complaining about missing unwind symbols without it, so I'm guessing they're using the wrong variable in their build system. - CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBARARY: The cmake try-compile was trying to dynamically link and complaining about the missing crtbeginS.o and crtendS.o, which we shouldn't need since this is only for statically linking. One thing to note is that this will cause CMake symbol checks to emit the wrong result, saying that the symbol is always available. https://gitlab.kitware.com/cmake/cmake/-/issues/18121 Fixing the variable name in the toolchain file to specify the triple correctly. Also adding flags for hooking up the Swift-Foundation library sources. * More cleanups This patch is just general clean-ups that make iterating with the script locally a bit easier.
1 parent 2ed3e02 commit f3d451e

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

swift-ci/sdks/static-linux/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,3 @@ RUN groupadd -g 998 build-user && \
9191
USER build-user
9292

9393
WORKDIR /home/build-user
94-
95-
96-
97-

swift-ci/sdks/static-linux/scripts/build.sh

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ while [ "$#" -gt 0 ]; do
136136
archs="$2"; shift ;;
137137
--version)
138138
static_linux_sdk_version="$2"; shift ;;
139+
-j|--jobs)
140+
parallel_jobs=$2; shift ;;
139141
*)
140-
usage; exit 0 ;;
142+
echo "Unknown argument '$1'"; usage; exit 0 ;;
141143
esac
142144
shift
143145
done
@@ -232,9 +234,16 @@ function run() {
232234
header "Building CMake from source"
233235

234236
quiet_pushd ${source_dir}/swift-project/cmake
235-
run ./bootstrap --no-qt-gui -- -DCMAKE_USE_OPENSSL=OFF
236-
run make -j$parallel_jobs
237-
run export PATH=${source_dir}/swift-project/cmake/bin:$PATH
237+
run cmake -G 'Ninja' ./ \
238+
-B ${source_dir}/swift-project/cmake/build \
239+
-DCMAKE_INSTALL_PREFIX=${source_dir}/swift-project/cmake/install \
240+
-DCMAKE_BUILD_TYPE=Release \
241+
-DCMAKE_USE_OPENSSL=OFF \
242+
-DBUILD_CursesDialog=OFF \
243+
-DBUILD_TESTING=OFF
244+
run ninja -C ${source_dir}/swift-project/cmake/build
245+
run ninja -C ${source_dir}/swift-project/cmake/build install
246+
run export PATH="${source_dir}/swift-project/cmake/install/bin:$PATH"
238247
quiet_popd
239248
run cmake --version
240249

@@ -283,7 +292,7 @@ for arch in $archs; do
283292
cp -rT $clang_resource_dir/include $sdk_resource_dir/include
284293

285294
cc="${swift_dir}/bin/clang -target $triple -resource-dir ${sdk_resource_dir} --sysroot ${sdk_root}"
286-
cxx="${swift_dir}/bin/clang++ -target $triple -resource-dir ${sdk_resource_dir} --sysroot ${sdk_root}"
295+
cxx="${swift_dir}/bin/clang++ -target $triple -resource-dir ${sdk_resource_dir} --sysroot ${sdk_root} -stdlib=libc++ -unwindlib=libunwind"
287296
as="$cc"
288297

289298
# Creating this gets rid of a warning
@@ -337,7 +346,7 @@ EOF
337346
echo "Fixing $header"
338347
sed -i -E "s:#define[ \t]+__NEED_([_A-Za-z][_A-Za-z0-9]*):#include <bits/types/\1.h>:g;/#include <bits\/alltypes.h>/d" $header
339348
done
340-
mkdir _modules
349+
mkdir -p _modules
341350
for header in assert complex ctype errno fenv float inttypes iso646 \
342351
limits locale math setjmp stdalign stdarg stdatomic \
343352
stdbool stddef stdint stdio stdlib string tgmath \
@@ -391,14 +400,14 @@ set(CMAKE_SYSTEM_PROCESSOR ${arch})
391400
set(CMAKE_ASM_COMPILER_TARGET ${triple})
392401
set(CMAKE_C_COMPILER_TARGET ${triple})
393402
set(CMAKE_CXX_COMPILER_TARGET ${triple})
394-
set(CMAKE_SWIFT_COMPILER_TARGET ${triple})
403+
set(CMAKE_Swift_COMPILER_TARGET ${triple})
395404
set(CMAKE_SYSROOT ${sdk_root})
396405
397406
set(CMAKE_CROSSCOMPILING=YES)
398-
set(CMAKE_EXE_LINKER_FLAGS "-rtlib=compiler-rt -stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi")
407+
set(CMAKE_EXE_LINKER_FLAGS "-unwindlib=libunwind -rtlib=compiler-rt -stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi")
399408
400409
set(CMAKE_C_COMPILER ${swift_dir}/bin/clang -resource-dir ${sdk_resource_dir})
401-
set(CMAKE_CXX_COMPILER ${swift_dir}/bin/clang++ -resource-dir ${sdk_resource_dir})
410+
set(CMAKE_CXX_COMPILER ${swift_dir}/bin/clang++ -resource-dir ${sdk_resource_dir} -stdlib=libc++)
402411
set(CMAKE_ASM_COMPILER ${swift_dir}/bin/clang -resource-dir ${sdk_resource_dir})
403412
set(CMAKE_FIND_ROOT_PATH ${sdk_root})
404413
EOF
@@ -414,6 +423,7 @@ EOF
414423
-G Ninja \
415424
-DCMAKE_TOOLCHAIN_FILE=${build_dir}/$arch/toolchain.cmake \
416425
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \
426+
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
417427
-DCOMPILER_RT_BUILD_BUILTINS=ON \
418428
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
419429
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
@@ -483,6 +493,9 @@ EOF
483493
run ninja -j$parallel_jobs install
484494
quiet_popd
485495

496+
ldflags="-fuse-ld=lld -rtlib=compiler-rt -unwindlib=libunwind"
497+
cxxldflags="-fuse-ld=lld -rtlib=compiler-rt -unwind=libunwind -lc++ -lc++abi"
498+
486499
# -----------------------------------------------------------------------
487500

488501
header "Building zlib for $arch"
@@ -492,6 +505,8 @@ EOF
492505
run /bin/env \
493506
CC="$cc" \
494507
CXX="$cxx" \
508+
LDFLAGS="$ldflags" \
509+
CXXLDFLAGS="$cxxldflags" \
495510
AS="$as" \
496511
AR="ar" RANLIB="ranlib" \
497512
"${source_dir}/zlib/configure" \
@@ -521,6 +536,7 @@ EOF
521536
--enable-strict --disable-icuio \
522537
--disable-plugins --disable-dyload --disable-extras \
523538
--disable-samples --disable-layoutex --with-data-packaging=auto \
539+
LDFLAGS="$cxxldflags" \
524540
CC="$cc" \
525541
CXX="$cxx" \
526542
AS="$as" \
@@ -532,7 +548,7 @@ EOF
532548
mkdir -p ${sdk_root}/usr/lib/swift_static/linux-static
533549
quiet_pushd ${sdk_root}/usr/lib/swift_static/linux-static
534550
for library in data i18n test tu uc; do
535-
ln -s ../../libicu${library}.a libicu${library}swift.a
551+
ln -sf ../../libicu${library}.a libicu${library}swift.a
536552
done
537553
quiet_popd
538554

@@ -542,7 +558,7 @@ EOF
542558

543559
run cmake -G Ninja -S ${source_dir}/libxml2 -B ${build_dir}/$arch/libxml2 \
544560
-DCMAKE_TOOLCHAIN_FILE=${build_dir}/$arch/toolchain.cmake \
545-
-DCMAKE_EXTRA_LINK_FLAGS="-rtlib=compiler-rt -stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi" \
561+
-DCMAKE_EXTRA_LINK_FLAGS="-rtlib=compiler-rt -unwindlib=libunwind -stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi" \
546562
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
547563
-DCMAKE_INSTALL_PREFIX=$sdk_root/usr \
548564
-DBUILD_SHARED_LIBS=NO \
@@ -644,14 +660,13 @@ EOF
644660
run ${source_dir}/swift-project/swift/utils/build-script -r \
645661
--reconfigure \
646662
--compiler-vendor=apple \
647-
--bootstrapping bootstrapping \
663+
--bootstrapping hosttools \
648664
--build-linux-static --install-swift \
649665
--stdlib-deployment-targets linux-x86_64,linux-static-$arch \
650666
--build-stdlib-deployment-targets all \
651667
--musl-path=${build_dir}/sdk_root \
652668
--linux-static-arch=$arch \
653669
--install-destdir=$sdk_root \
654-
--cmake=${source_dir}/swift-project/cmake/bin/cmake \
655670
--install-prefix=/usr \
656671
--swift-install-components="libexec;stdlib;sdk-overlay" \
657672
--extra-cmake-options="-DSWIFT_SHOULD_BUILD_EMBEDDED_STDLIB=NO -DLLVM_USE_LINKER=lld -DLLVM_NO_DEAD_STRIP=On" \
@@ -662,7 +677,7 @@ EOF
662677
lld=$(find ${build_dir}/swift -name 'ld.lld')
663678

664679
# Add the Swift compiler to the CMake toolchain file
665-
cat >> build/$arch/toolchain.cmake <<EOF
680+
cat >> ${build_dir}/$arch/toolchain.cmake <<EOF
666681
set(CMAKE_Swift_FLAGS "-static-stdlib -use-ld=${lld} -sdk ${sdk_root} -target ${arch}-swift-linux-musl -resource-dir ${sdk_root}/usr/lib/swift_static -Xclang-linker -resource-dir -Xclang-linker ${sdk_resource_dir}")
667682
set(CMAKE_Swift_COMPILER ${swiftc})
668683
EOF
@@ -682,6 +697,7 @@ EOF
682697
-DCMAKE_REQUIRED_DEFINITIONS=-D_GNU_SOURCE \
683698
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
684699
-DCMAKE_INSTALL_PREFIX=$sdk_root/usr \
700+
-DCMAKE_Swift_COMPILER_WORKS=YES \
685701
-DBUILD_SHARED_LIBS=NO \
686702
-DENABLE_SWIFT=YES \
687703
-DSWIFT_SYSTEM_NAME=linux-static
@@ -706,6 +722,9 @@ EOF
706722
-DSWIFT_SYSTEM_NAME=linux-static \
707723
-DFOUNDATION_PATH_TO_LIBDISPATCH_SOURCE=${source_dir}/swift-project/swift-corelibs-libdispatch \
708724
-DFOUNDATION_PATH_TO_LIBDISPATCH_BUILD=${build_dir}/$arch/dispatch \
725+
-D_SwiftFoundation_SourceDIR=${source_dir}/swift-project/swift-foundation \
726+
-D_SwiftFoundationICU_SourceDIR=${source_dir}/swift-project/swift-foundation-icu \
727+
-DCMAKE_Swift_COMPILER_WORKS=YES \
709728
-Ddispatch_DIR=${build_dir}/$arch/dispatch/cmake/modules
710729

711730
quiet_pushd ${build_dir}/$arch/foundation
@@ -924,4 +943,3 @@ quiet_pushd "${build_dir}"
924943
mkdir -p "${products_dir}"
925944
tar cvzf "${products_dir}/${bundle}.tar.gz" "${bundle}"
926945
quiet_popd
927-

0 commit comments

Comments
 (0)