Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cross-compiling #1398

Closed
wants to merge 2 commits into from
Closed

Enable cross-compiling #1398

wants to merge 2 commits into from

Conversation

froody
Copy link
Contributor

@froody froody commented Feb 22, 2016

Allow using and OS X build of swift to build libraries and binaries for another
platform, only linux-armv7 is supported in this patch. Doesn't currently support
building the tools (llvm, swiftc) or the tests for linux-armv7.

  • Added --cross-compile-stdlib-targets to build-script
  • Added --cross-compile-sysroot to build-script-impl
  • Added --cross-compile-toolchain-bin to build-script-impl
  • Set -resource-dir when cross-compiling
  • Use BUILD_HOST_TARGET instead of HOST_TARGET, allow it to be empty when
    cross-compiling to just build cross target
  • Derive PKG_CONFIG_PATH from --cross-compile-sysroot to allow find_package to
    work
  • Skip cmark and llvm builds completely when --native-*-tools-path flags are set

See instructions in cmake/modules/Toolchain-linux-arm.cmake. To build
linux-armv7 on osx run:

./utils/build-script -R --cross-compile-stdlib-targets linux-armv7 -- --cross-compile-sysroot=$MY_SYSROOT --cross-compile-toolchain-bin=$MY_TOOLCHAIN_BIN

@gribozavr
Copy link
Collaborator

Why not use debootstrap instead of the ad-hoc make_sysroot script?

@froody
Copy link
Contributor Author

froody commented Feb 22, 2016

It was more work to figure out how to install/run debootstrap on OS X to do what I wanted, and provide easy-to-follow instructions, than to write this script. Would it be better if I just removed the script and hosted it myself, and suggest that users provide their own sysroot?

@froody froody force-pushed the cross-compile branch 2 times, most recently from 246850b to dd4f01a Compare February 23, 2016 00:31
@gribozavr
Copy link
Collaborator

debootstrap is the official ways to create sysroots for Debian and derivatives. I'd rather we don't promote a hacky way.

@gribozavr
Copy link
Collaborator

If someone needs to run debootstrap on OS X, they can easily use a VM, for example.

@froody
Copy link
Contributor Author

froody commented Feb 23, 2016

Ok, I removed my script, any thoughts on the rest of the patch?

@froody
Copy link
Contributor Author

froody commented Feb 25, 2016

@gribozavr ping?

@cellularmitosis
Copy link

I'm having a bit of trouble getting this pull request to work.

I've used your pull request as a patch against the 2/26 swift snapshot, and built up a set of scripts according to your instructions: https://github.com/cellularmitosis/swift-build-scripts

UPDATE: ran this again from scratch, different error:

+ ./swift/utils/build-script -R --cross-compile-targets linux-armv7 -- --cross-compile-sysroot=/home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty --cross-compile-toolchain-bin=/home/cell/swift.20160226.2/swift-build-scripts/stage/gcc-arm-none-eabi-4_9-2015q3/bin
Building the standard library for: swift-test-stdlib-linux-x86_64

swift: using gold linker
+ cd /home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7
+ /usr/bin/cmake -G Ninja -DCMAKE_C_COMPILER:PATH=/usr/bin/clang -DCMAKE_CXX_COMPILER:PATH=/usr/bin/clang++ -DCMAKE_TOOLCHAIN_FILE=/home/cell/swift.20160226.2/swift-build-scripts/stage/swift/cmake/modules/Toolchain-linux-arm.cmake -DLLVM_NATIVE_BUILD=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin/../ -DCMAKE_SYSROOT:PATH=/home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty -DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/gcc-arm-none-eabi-4_9-2015q3/bin -DSWIFT_ENABLE_GOLD_LINKER=TRUE -DLLVM_MAIN_SRC_DIR:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/llvm '-DCMAKE_C_FLAGS= -fno-stack-protector' '-DCMAKE_CXX_FLAGS= -fno-stack-protector' -DCMAKE_BUILD_TYPE:STRING=Release -DLLVM_ENABLE_ASSERTIONS:BOOL=TRUE -DSWIFT_ANALYZE_CODE_COVERAGE:STRING=FALSE -DSWIFT_STDLIB_BUILD_TYPE:STRING=Release -DSWIFT_STDLIB_ASSERTIONS:BOOL=TRUE -DSWIFT_STDLIB_ENABLE_RESILIENCE:BOOL=FALSE -DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin -DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin -DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin -DSWIFT_BUILD_TOOLS:BOOL=FALSE -DSWIFT_BUILD_STDLIB:BOOL=TRUE -DSWIFT_SERIALIZE_STDLIB_UNITTEST:BOOL=FALSE -DSWIFT_BUILD_SDK_OVERLAY:BOOL=TRUE -DSWIFT_BUILD_STATIC_STDLIB:BOOL=FALSE -DSWIFT_BUILD_PERF_TESTSUITE:BOOL=FALSE -DSWIFT_BUILD_EXAMPLES:BOOL=TRUE -DSWIFT_INCLUDE_TESTS:BOOL=FALSE -DSWIFT_INSTALL_COMPONENTS:STRING= -DSWIFT_EMBED_BITCODE_SECTION:BOOL=FALSE -DSWIFT_ENABLE_LTO:BOOL=FALSE -DSWIFT_DARWIN_XCRUN_TOOLCHAIN:STRING=default -DSWIFT_AST_VERIFIER:BOOL=TRUE -DSWIFT_SIL_VERIFY_ALL:BOOL=FALSE -DSWIFT_RUNTIME_ENABLE_DTRACE:BOOL=FALSE -DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER:BOOL=FALSE -DCMAKE_INSTALL_PREFIX:PATH=/usr -DLLVM_CONFIG:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7/bin/llvm-config -DSWIFT_PATH_TO_CLANG_SOURCE:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/llvm/tools/clang -DSWIFT_PATH_TO_CLANG_BUILD:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7 -DSWIFT_PATH_TO_LLVM_SOURCE:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/llvm -DSWIFT_PATH_TO_LLVM_BUILD:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7 -DSWIFT_PATH_TO_CMARK_SOURCE:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/cmark -DSWIFT_PATH_TO_CMARK_BUILD:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/cmark-linux-armv7 -DSWIFT_CMARK_LIBRARY_DIR:PATH=/home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/cmark-linux-armv7/src /home/cell/swift.20160226.2/swift-build-scripts/stage/swift
-- Using Linux-arm toolchain file
-- Using Linux-arm toolchain file
-- The ASM compiler identification is Clang
-- Found assembler: /usr/bin/clang
-- Performing Test LLVM_NO_OLD_LIBSTDCXX
-- Performing Test LLVM_NO_OLD_LIBSTDCXX - Success
-- Performing Test C_SUPPORTS_FPIC
-- Performing Test C_SUPPORTS_FPIC - Success
-- Performing Test CXX_SUPPORTS_FPIC
-- Performing Test CXX_SUPPORTS_FPIC - Success
-- Building with -fPIC
-- Performing Test SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG
-- Performing Test SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG - Success
-- Performing Test CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG
-- Performing Test CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG - Success
-- Performing Test C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG
-- Performing Test C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG - Success
-- Performing Test CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG
-- Performing Test CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG - Success
-- Performing Test CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
-- Performing Test CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR - Success
-- Performing Test C_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG
-- Performing Test C_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG - Success
-- Performing Test CXX_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG
-- Performing Test CXX_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG - Success
-- Performing Test C_WCOMMENT_ALLOWS_LINE_WRAP
-- Performing Test C_WCOMMENT_ALLOWS_LINE_WRAP - Success
-- Performing Test C_SUPPORTS_WERROR_DATE_TIME
-- Performing Test C_SUPPORTS_WERROR_DATE_TIME - Success
-- Performing Test CXX_SUPPORTS_WERROR_DATE_TIME
-- Performing Test CXX_SUPPORTS_WERROR_DATE_TIME - Success
-- Performing Test CXX_SUPPORTS_CXX11
-- Performing Test CXX_SUPPORTS_CXX11 - Success
-- Performing Test C_SUPPORTS_FNO_FUNCTION_SECTIONS
-- Performing Test C_SUPPORTS_FNO_FUNCTION_SECTIONS - Success
-- Performing Test C_SUPPORTS_FFUNCTION_SECTIONS
-- Performing Test C_SUPPORTS_FFUNCTION_SECTIONS - Success
-- Performing Test CXX_SUPPORTS_FFUNCTION_SECTIONS
-- Performing Test CXX_SUPPORTS_FFUNCTION_SECTIONS - Success
-- Performing Test C_SUPPORTS_FDATA_SECTIONS
-- Performing Test C_SUPPORTS_FDATA_SECTIONS - Success
-- Performing Test CXX_SUPPORTS_FDATA_SECTIONS
-- Performing Test CXX_SUPPORTS_FDATA_SECTIONS - Success
-- Performing Test result
-- Performing Test result - Failed
-- Performing Test CXX_SUPPORTS_DOCUMENTATION_FLAG
-- Performing Test CXX_SUPPORTS_DOCUMENTATION_FLAG - Success
-- Performing Test CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG
-- Performing Test CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG - Success
-- Performing Test CXX_SUPPORTS_UNREACHABLE_CODE_FLAG
-- Performing Test CXX_SUPPORTS_UNREACHABLE_CODE_FLAG - Success
-- Performing Test CXX_SUPPORTS_OVERLOADED_VIRTUAL
-- Performing Test CXX_SUPPORTS_OVERLOADED_VIRTUAL - Success
-- Performing Test CXX_SUPPORTS_FAPPLICATION_EXTENSION
-- Performing Test CXX_SUPPORTS_FAPPLICATION_EXTENSION - Failed
-- Linux SDK:
--   Path: /
--   Version: don't use
--   Build number: don't use
--   Deployment version: don't use
--   Library subdir: linux
--   Version min name: 
--   Triple name: linux
--   Architectures: armv7
--   Triple for armv7 is armv7-unknown-linux-gnueabihf
-- 
-- Building host Swift tools for LINUX armv7
--   Build type: Release
--   Assertions: TRUE
-- 
-- Building Swift standard library and SDK overlays for SDKs: LINUX
--   Build type: Release
--   Assertions: TRUE
-- 
-- Building Swift runtime with:
--   Dtrace:                             FALSE
--   Leak Detection Checker Entrypoints: FALSE
-- 
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.1") 
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.28") 
-- Found BSD: /home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/include  
-- Performing Test CXX_SUPPORTS_GLOBAL_CONSTRUCTORS_WARNING
-- Performing Test CXX_SUPPORTS_GLOBAL_CONSTRUCTORS_WARNING - Success
-- Performing Test CXX_SUPPORTS_EXIT_TIME_DESTRUCTORS_WARNING
-- Performing Test CXX_SUPPORTS_EXIT_TIME_DESTRUCTORS_WARNING - Success
-- Performing Test CXX_SUPPORTS_DEFAULT_HIDDEN_VISIBILITY
-- Performing Test CXX_SUPPORTS_DEFAULT_HIDDEN_VISIBILITY - Success
-- Found ICU: /home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/include/arm-linux-gnueabihf  
CMake Warning at docs/CMakeLists.txt:24 (message):
  Unable to find sphinx-build program.  Not building docs


CMake Warning at docs/CMakeLists.txt:130 (message):
  LitRe not found; code examples won't be tested.


-- Configuring done
CMake Warning (dev) at cmake/modules/AddSwift.cmake:1477 (add_dependencies):
  Policy CMP0046 is not set: Error on non-existent dependency in
  add_dependencies.  Run "cmake --help-policy CMP0046" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  The dependency target
  "/home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/arm-linux-gnueabihf/libicui18n.so-linux"
  of target "swiftStdlibStubs-linux-armv7" does not exist.
Call Stack (most recent call first):
  stdlib/public/stubs/CMakeLists.txt:22 (add_swift_library)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at cmake/modules/AddSwift.cmake:1477 (add_dependencies):
  Policy CMP0046 is not set: Error on non-existent dependency in
  add_dependencies.  Run "cmake --help-policy CMP0046" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  The dependency target
  "/home/cell/swift.20160226.2/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/arm-linux-gnueabihf/libicuuc.so-linux"
  of target "swiftStdlibStubs-linux-armv7" does not exist.
Call Stack (most recent call first):
  stdlib/public/stubs/CMakeLists.txt:22 (add_swift_library)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    LLVM_CONFIG
    LLVM_NATIVE_BUILD
    SWIFT_ANALYZE_CODE_COVERAGE
    SWIFT_AST_VERIFIER
    SWIFT_BUILD_EXAMPLES
    SWIFT_DARWIN_XCRUN_TOOLCHAIN
    SWIFT_NATIVE_CLANG_TOOLS_PATH


-- Build files have been written to: /home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7
++ cmake_config_opt swift
++ product=swift
++ [[ Ninja == \X\c\o\d\e ]]
+ /usr/bin/cmake --build /home/cell/swift.20160226.2/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7 -- -j4 all swift-test-stdlib-linux-x86_64
ninja: error: unknown target 'swift-test-stdlib-linux-x86_64'
./swift/utils/build-script: command terminated with a non-zero exit status 1, aborting

I noticed that indeed, libicui18n.so-linux and libicuuc.so-linux do not exist, but libicui18n.so and libicuuc.so-linux do.

@froody
Copy link
Contributor Author

froody commented Feb 28, 2016

Are you building this on a Linux machine? It looks like it from this log. I haven't accounted for that in this patch, but can. You need to add "linux-armv7" to the STDLIB_DEPLOYMENT_TARGETS for your host (see where STDLIB_DEPLOYMENT_TARGETS is first assigned in build-script-impl) and disable the host build somehow (an easy hacky way to see if it works is to comment out the "host" stdlib deployment target, so STDLIB_DEPLOYMENT_TARGETS=("linux-armv7"). I'll work on getting linux-x86_64 cross-compiling linux-armv7 into this patch too.

@cellularmitosis
Copy link

That would be amazing if you could also work on linux-x86_64! I'll also try your suggested workaround in the mean time.

targets_group.add_argument(
"--cross-compile-targets",
help="The cross-compliation targets. The Swift stdlib will be built for"
"these targets using the host-target LLVM and Clang",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description does not sound correct to me. We can't build the Swift standard library using LLVM and Clang, we need a Swift compiler.

@dabrahams
Copy link
Collaborator

FYI, I’m waiting to hear from @cellularmitosis that this has worked for him before proceeding to evaluate it.

@gribozavr
Copy link
Collaborator

Invoking the build-script-impl twice from the top-level build-script is not a good solution either. build-script-impl already has support for invoking cmake multiple times, it should be able to figure out everything.

@cellularmitosis
Copy link

I'll fire off another build this evening.

update (6:53pm):

building using the following invocation:

sysroot="`pwd`/sysroots/sysroot.armhf.ubuntu.trusty"
toolchain="`pwd`/stage/gcc-arm-none-eabi-4_9-2015q3/bin"

cd stage

./swift/utils/build-script -R \
    --cross-compile-stdlib-targets linux-armv7 \
    -- \
    --cross-compile-sysroot="${sysroot}" \
    --cross-compile-toolchain-bin="${toolchain}"

update: 8:02pm:

Looks like it failed in the same way:

  The dependency target
  "/home/cell/swift.20160229.cross1/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/arm-linux-gnueabihf/libicuuc.so-linux"
  of target "swiftStdlibStubs-linux-armv7" does not exist.
Call Stack (most recent call first):
  stdlib/public/stubs/CMakeLists.txt:22 (add_swift_library)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    LLVM_CONFIG
    LLVM_NATIVE_BUILD
    SWIFT_ANALYZE_CODE_COVERAGE
    SWIFT_AST_VERIFIER
    SWIFT_BUILD_EXAMPLES
    SWIFT_DARWIN_XCRUN_TOOLCHAIN
    SWIFT_NATIVE_CLANG_TOOLS_PATH


-- Build files have been written to: /home/cell/swift.20160229.cross1/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7
++ cmake_config_opt swift
++ product=swift
++ [[ Ninja == \X\c\o\d\e ]]
+ /usr/bin/cmake --build /home/cell/swift.20160229.cross1/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7 -- -j4 all swift-test-stdlib-linux-x86_64
ninja: error: unknown target 'swift-test-stdlib-linux-x86_64'
./swift/utils/build-script: command terminated with a non-zero exit status 1, aborting

I'm invoking this script: https://github.com/cellularmitosis/swift-build-scripts/blob/master/go-linux-armv7.sh

Which builds against the swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a tarball releases after applying the 1398 pull request as a patch (https://github.com/cellularmitosis/swift-build-scripts/blob/master/patches/1398.patch)

@froody Did you still want me to do the manual modifications you mention above? How should I invoke this build?

Thanks,
Jason

@froody froody force-pushed the cross-compile branch 3 times, most recently from 08799ad to 53a72a5 Compare March 1, 2016 20:58
@froody
Copy link
Contributor Author

froody commented Mar 1, 2016

Updated to fix linux build.

@cellularmitosis: I successfully built on ubuntu 15.10/x86_64 with
./utils/build-script -R --cross-compile-stdlib-targets linux-armv7 -- --reconfigure --cross-compile-sysroot=~/swifty/sysroot/armv7-sysroot/ --cross-compile-toolchain-bin=/usr/bin/arm-linux-gnueabi-
can you try that with this latest patch? You may need to install gcc-arm-linux-gnueabi

@cellularmitosis
Copy link

Thanks! Rebuilding. I'll update this comment when the build finishes.

update

@froody the build failed on step 7 of building the standard lib. I'm guessing this was the first linking step. It looks like it couldn't find ar.

-- Build files have been written to: /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7
++ cmake_config_opt swift
++ product=swift
++ [[ Ninja == \X\c\o\d\e ]]
+ /usr/bin/cmake --build /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7 -- -j4 all swift-test-stdlib-linux-armv7
[7/86] Linking CXX static library lib/...t/linux/armv7/libsection_magic_begin.a
FAILED: : && /usr/bin/cmake -E remove lib/swift/linux/armv7/libsection_magic_begin.a && CMAKE_AR-NOTFOUND cr lib/swift/linux/armv7/libsection_magic_begin.a  stdlib/public/runtime/CMakeFiles/section_magic_begin-linux-armv7.dir/swift_sections.S.o && : lib/swift/linux/armv7/libsection_magic_begin.a && :
/bin/sh: 1: CMAKE_AR-NOTFOUND: not found
[7/86] Linking CXX static library lib/...ift/linux/armv7/libsection_magic_end.a
FAILED: : && /usr/bin/cmake -E remove lib/swift/linux/armv7/libsection_magic_end.a && CMAKE_AR-NOTFOUND cr lib/swift/linux/armv7/libsection_magic_end.a  stdlib/public/runtime/CMakeFiles/section_magic_end-linux-armv7.dir/swift_sections.S.o && : lib/swift/linux/armv7/libsection_magic_end.a && :
/bin/sh: 1: CMAKE_AR-NOTFOUND: not found
[7/86] Building CXX object stdlib/publ...tRuntime-linux-armv7.dir/Casting.cpp.o
ninja: build stopped: subcommand failed.
./swift/utils/build-script: command terminated with a non-zero exit status 1, aborting

real    62m49.016s
user    216m39.524s
sys     4m6.152s

This was invoked via https://github.com/cellularmitosis/swift-build-scripts/blob/master/build-linux-armv7.sh

I'm using the toolchain from https://launchpad.net/gcc-arm-embedded/4.9/4.9-2015-q3-update/+download/gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2

I performed the following grep, perhaps it is useful?

cell@hp(master)$ [130] grep -r 'set(CMAKE_AR ' .
./build/Ninja-ReleaseAssert/swift-linux-x86_64/CMakeFiles/3.0.2/CMakeASMCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/swift-linux-x86_64/CMakeFiles/3.0.2/CMakeCCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/swift-linux-x86_64/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/3.0.2/CMakeASMCompiler.cmake:set(CMAKE_AR "CMAKE_AR-NOTFOUND")
./build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/3.0.2/CMakeCCompiler.cmake:set(CMAKE_AR "CMAKE_AR-NOTFOUND")
./build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake:set(CMAKE_AR "CMAKE_AR-NOTFOUND")
./build/Ninja-ReleaseAssert/llvm-linux-x86_64/CMakeFiles/3.0.2/CMakeASMCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/llvm-linux-x86_64/CMakeFiles/3.0.2/CMakeCCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/llvm-linux-x86_64/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/cmark-linux-x86_64/CMakeFiles/3.0.2/CMakeCCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./build/Ninja-ReleaseAssert/cmark-linux-x86_64/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake:set(CMAKE_AR "/usr/bin/ar")
./swift/cmake/modules/Toolchain-linux-arm.cmake:  set(CMAKE_AR ${CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN}/arm-none-eabi-ar CACHE FILEPATH "Archiver") # https://cmake.org/Bug/view.php?id=13038

@froody
Copy link
Contributor Author

froody commented Mar 2, 2016

@cellularmitosis Can you change your script to set:

toolchain="`pwd`/stage/gcc-arm-none-eabi-4_9-2015q3/bin/arm-none-eabi-"

and delete the ./build/Ninja-ReleaseAssert/swift-linux-armv7/ directory and retry?

@cellularmitosis
Copy link

Ah, now I understand.

I'll try again.

update @froody

A different failure:

-- Generating done
-- Build files have been written to: /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-x86_64
++ cmake_config_opt swift
++ product=swift
++ [[ Ninja == \X\c\o\d\e ]]
+ /usr/bin/cmake --build /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-x86_64 -- -j4 all swift-test-stdlib-linux-x86_64
[1/1] Symlinking Clang resource headers into /home/cell/swift.20160301.cross3/swif...build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-x86_64/./lib/swift/clang
Building the standard library for: swift-test-stdlib-linux-armv7

swift: using gold linker
+ cd /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7
+ /usr/bin/cmake -G Ninja -DCMAKE_C_COMPILER:PATH=/usr/bin/clang -DCMAKE_CXX_COMPILER:PATH=/usr/bin/clang++ -DCMAKE_TOOLCHAIN_FILE=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/swift/cmake/modules/Toolchain-linux-arm.cmake -DLLVM_NATIVE_BUILD=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin/../ -DCMAKE_SYSROOT:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty -DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/gcc-arm-none-eabi-4_9-2015q3/bin/arm-none-eabi- -DSWIFT_ENABLE_GOLD_LINKER=TRUE -DLLVM_MAIN_SRC_DIR:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/llvm '-DCMAKE_C_FLAGS= -fno-stack-protector' '-DCMAKE_CXX_FLAGS= -fno-stack-protector' -DCMAKE_BUILD_TYPE:STRING=Release -DLLVM_ENABLE_ASSERTIONS:BOOL=TRUE -DSWIFT_ANALYZE_CODE_COVERAGE:STRING=FALSE -DSWIFT_STDLIB_BUILD_TYPE:STRING=Release -DSWIFT_STDLIB_ASSERTIONS:BOOL=TRUE -DSWIFT_STDLIB_ENABLE_RESILIENCE:BOOL=FALSE -DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin -DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bin -DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin -DSWIFT_BUILD_TOOLS:BOOL=FALSE -DSWIFT_BUILD_STDLIB:BOOL=TRUE -DSWIFT_SERIALIZE_STDLIB_UNITTEST:BOOL=FALSE -DSWIFT_BUILD_SDK_OVERLAY:BOOL=TRUE -DSWIFT_BUILD_STATIC_STDLIB:BOOL=FALSE -DSWIFT_BUILD_PERF_TESTSUITE:BOOL=FALSE -DSWIFT_BUILD_EXAMPLES:BOOL=TRUE -DSWIFT_INCLUDE_TESTS:BOOL=FALSE -DSWIFT_INSTALL_COMPONENTS:STRING= -DSWIFT_EMBED_BITCODE_SECTION:BOOL=FALSE -DSWIFT_ENABLE_LTO:BOOL=FALSE -DSWIFT_DARWIN_XCRUN_TOOLCHAIN:STRING=default -DSWIFT_AST_VERIFIER:BOOL=TRUE -DSWIFT_SIL_VERIFY_ALL:BOOL=FALSE -DSWIFT_RUNTIME_ENABLE_DTRACE:BOOL=FALSE -DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER:BOOL=FALSE -DCMAKE_INSTALL_PREFIX:PATH=/usr -DLLVM_CONFIG:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7/bin/llvm-config -DSWIFT_PATH_TO_CLANG_SOURCE:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/llvm/tools/clang -DSWIFT_PATH_TO_CLANG_BUILD:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7 -DSWIFT_PATH_TO_LLVM_SOURCE:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/llvm -DSWIFT_PATH_TO_LLVM_BUILD:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-armv7 -DSWIFT_PATH_TO_CMARK_SOURCE:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/cmark -DSWIFT_PATH_TO_CMARK_BUILD:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/cmark-linux-armv7 -DSWIFT_CMARK_LIBRARY_DIR:PATH=/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/cmark-linux-armv7/src /home/cell/swift.20160301.cross3/swift-build-scripts/stage/swift
-- Using Linux-arm toolchain file
-- Using Linux-arm toolchain file
-- The ASM compiler identification is Clang
-- Found assembler: /usr/bin/clang
-- Performing Test LLVM_NO_OLD_LIBSTDCXX
-- Performing Test LLVM_NO_OLD_LIBSTDCXX - Failed
CMake Error at /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/llvm-linux-x86_64/share/llvm/cmake/HandleLLVMOptions.cmake:43 (message):
  Host Clang must be able to find libstdc++4.7 or newer!
Call Stack (most recent call first):
  cmake/modules/SwiftSharedCMakeConfig.cmake:231 (include)
  CMakeLists.txt:291 (swift_common_standalone_build_config)


-- Configuring incomplete, errors occurred!
See also "/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/CMakeOutput.log".
See also "/home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/CMakeError.log".
./swift/utils/build-script: command terminated with a non-zero exit status 1, aborting

CMakeError.log contains:

cell@hp(master)$ cat /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/CMakeError.log
Performing C++ SOURCE FILE Test LLVM_NO_OLD_LIBSTDCXX failed with the following output:
Change Dir: /home/cell/swift.20160301.cross3/swift-build-scripts/stage/build/Ninja-ReleaseAssert/swift-linux-armv7/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/ninja" "cmTryCompileExec3418794998"
[1/2] Building CXX object CMakeFiles/cmTryCompileExec3418794998.dir/src.cxx.o
[2/2] Linking CXX executable cmTryCompileExec3418794998
FAILED: : && /usr/bin/clang++  --target=arm-linux-gnueabihf --sysroot=/home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty  -fno-stack-protector -B /home/cell/swift.20160301.cross3/swift-build-scripts/stage/gcc-arm-none-eabi-4_9-2015q3/bin/arm-none-eabi- -B /home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8 -fuse-ld=gold -B /home/cell/swift.20160301.cross3/swift-build-scripts/stage/gcc-arm-none-eabi-4_9-2015q3/bin/arm-none-eabi- -B /home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8 -fuse-ld=gold -DLLVM_NO_OLD_LIBSTDCXX -std=c++0x   CMakeFiles/cmTryCompileExec3418794998.dir/src.cxx.o  -o cmTryCompileExec3418794998 -L/home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/arm-linux-gnueabihf  -L/home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8 -rdynamic && :
/usr/bin/ld.gold: fatal error: /home/cell/swift.20160301.cross3/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/crt1.o: unsupported ELF machine number 40
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Source file was:

#include <atomic>
std::atomic<float> x(0.0f);
int main() { return (float)x; }

It looks like the relevant error is:

/usr/bin/ld.gold: fatal error: /home/cell/.../crt1.o: unsupported ELF machine number 40

Should it be using /usr/bin/ld.gold at that point?

I just realized that the manner in which I'm copying in arm-linux-androideabi-ld.gold from the android NDK differs from the naming convention of the rest of the toolchain:

cell@hp(master)$ ls -1 stage/gcc-arm-none-eabi-4_9-2015q3/bin/ | tail
arm-none-eabi-ld.bfd
arm-none-eabi-nm
arm-none-eabi-objcopy
arm-none-eabi-objdump
arm-none-eabi-ranlib
arm-none-eabi-readelf
arm-none-eabi-size
arm-none-eabi-strings
arm-none-eabi-strip
ld.gold

Should I rename it to arm-none-eabi-ld.gold?

@cellularmitosis
Copy link

Hey @froody,

In the interest of speeding things up here, in an effort to more closely mirror the setup which has already worked for you, I ditched Debian in favor of Ubuntu 14.04, installed clang-3.6, gcc-arm-linux-gnueabi, and rebuilt.

I ran into the following error:

Run Build Command:/usr/bin/ninja cmTryCompileExec3944871647
[1/2] Building CXX object CMakeFiles/cmTryCompileExec3944871647.dir/src.cxx.o
[2/2] Linking CXX executable cmTryCompileExec3944871647
FAILED: : && /usr/bin/clang++-3.6   -fno-stack-protector -B /usr/bin/arm-linux-gnueabi- -B /home/cell/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8 -fuse-ld=gold -B /usr/bin/arm-linux-gnueabi- -B /home/cell/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8 -fuse-ld=gold -DLLVM_NO_OLD_LIBSTDCXX -std=c++0x   CMakeFiles/cmTryCompileExec3944871647.dir/src.cxx.o  -o cmTryCompileExec3944871647 -L/usr/lib/gcc/arm-linux-gnueabihf/4.8 -rdynamic && :
/usr/bin/arm-linux-gnueabi-ld.gold: error: /home/cell/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8/crtbegin.o: incompatible target
/usr/bin/arm-linux-gnueabi-ld.gold: error: /home/cell/swift-build-scripts/sysroots/sysroot.armhf.ubuntu.trusty/usr/lib/gcc/arm-linux-gnueabihf/4.8/crtend.o: incompatible target
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Source file was:

#include <atomic>
std::atomic<float> x(0.0f);
int main() { return (float)x; }

(this is in a VirtualBox VM).

@froody
Copy link
Contributor Author

froody commented Mar 4, 2016

@cellularmitosis I'm confused as to how you got this. I think what's missing is the "-target", but that should have been set by CMAKE_C_COMPILER_TARGET. I'm assuming you deleted the swift-linux-armv7 dir? Can you paste the whole build log?

@cellularmitosis
Copy link

@froody sure, after that failure I started a new build from scratch, to make sure the failure was reproducible. I'll post the results when I get home from work today.

update I forgot to give the VM sufficient swap space and the build failed during the linking phase. That's a great sign! I've increased the swap space and am building again.

@cellularmitosis
Copy link

@froody the from-scratch build failed:

Here's the entire build process output: https://gist.github.com/cellularmitosis/cc7fcb54a8e934c4df36

@karwa
Copy link
Contributor

karwa commented Mar 7, 2016

I get the same error, on OSX trying to build for Debian using the sysroot/toolchain script in swift-build-scripts.

@modocache
Copy link
Collaborator

Cross-compilation is something I'm extremely interested in: #1157 and other armv7 pull requests took a long time to test because contributors had to compile for hours on underpowered machines to get test results. I'm very excited for this pull request--thanks, @froody!

I tried this out myself and managed to get everything working on OS X, following the steps in make/modules/Toolchain-linux-arm.cmake. I ran into two snags, though--here's how I fixed them on my end:

  1. When I ran the make_sysroot.py script linked to in the documentation, it initially failed because I was missing a dpkg-deb executable in my PATH. brew install dpkg got it working.
  2. The documentation references utils/build-script -R --cross-compile-targets, but the correct option is now --cross-compile-stdlib-targets.

With the above I've successfully built the stdlib for linux-armv7 from an OS X machine:

$ file build/Ninja-ReleaseAssert/swift-linux-armv7/lib/swift/linux/libswiftCore.so
build/Ninja-ReleaseAssert/swift-linux-armv7/lib/swift/linux/libswiftCore.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

Exciting stuff! This would have made development on #1442 much simpler.

@dabrahams It'd be awesome if we could discuss whether this could be merged, I think it'd help a lot of people out (myself included).

@gribozavr
Copy link
Collaborator

@modocache One thing that the PR is not doing correctly now is invoking build-script-impl twice. build-script-impl already has support for cross-compilation, and this addition should fit into the existing scheme, so that we don't have two codepaths doing essentially the same, but slightly differently.

@dabrahams
Copy link
Collaborator

@modocache I’ll take a look again after you’ve addressed that double-invocation-of-build-script issue

Allow using and OS X build of swift to build libraries and binaries for another
platform, only linux-armv7 is supported in this patch. Doesn't currently support
building the tools (llvm, swiftc) or the tests for linux-armv7.

* Added --cross-compile-stdlib-targets to build-script
* Added --cross-compile-sysroot to build-script-impl
* Added --cross-compile-toolchain-bin to build-script-impl
* Set -resource-dir when cross-compiling
* Use BUILD_HOST_TARGET instead of HOST_TARGET, allow it to be empty when
  cross-compiling to just build cross target
* Derive PKG_CONFIG_PATH from --cross-compile-sysroot to allow find_package to
  work
* Skip cmark and llvm builds completely when --native-*-tools-path flags are set

See instructions in cmake/modules/Toolchain-linux-arm.cmake. To build
linux-armv7 on osx run:

./utils/build-script -R --cross-compile-stdlib-targets linux-armv7 --
--cross-compile-sysroot=$MY_SYSROOT
--cross-compile-toolchain-bin=$MY_TOOLCHAIN_BIN
@froody
Copy link
Contributor Author

froody commented Mar 7, 2016

@karwa Can you give me steps to recreate your sysroot?

@gribozavr @dabrahams I've addressed the double-invocation-of-build-script issue and updated the commit message/PR description

* Demux uname info into a list of stdlib_deployment_targets in python
* Generate list of allowed stdlib_cross_targets based on host info
@karwa
Copy link
Contributor

karwa commented Mar 11, 2016

OK, I've got it to compile on OSX. I compiled a new toolchain, just grab the source from here and edit the binutils build script to enable gold. I used the build output of that as my toolchain, but clang still couldn't resolve arm-none-eabi-ld.gold with the -fuse=gold option.

So I used the non-prefixed toolchain path (i.e. /arm-none-eabi/bin/ld.gold instead of /bin/arm-none-eabi-ld.gold) and that seemed to help it find everything. That means you need to change Toolchain-linux-arm.cmake and set AR path with "${CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN}/ar".

@stefanstefan2001
Copy link

What happend to this PR?

@modocache
Copy link
Collaborator

@stefanstefan2001 I'm trying to split it up (see #2187) and get cross-compilation merged. It seems like a sticking point is the multiple invocations of build-script-impl, so I'll try to address that. I'm considering a different approach than using CMake toolchain files, but I'm still figuring out what's possible.

@DougGregor
Copy link
Member

This PR has been dormant for a long time. Is it still relevant?

@dabrahams
Copy link
Collaborator

@DougGregor It's still relevant as long as build performance is cheaper to achieve on some platforms than on others.

@dcartman
Copy link

Any update on when this might be merged?

@slavapestov
Copy link
Member

Can someone rebase this onto master and fix the conflicts, and we'll make sure to review it and get it merged ASAP?

@slavapestov slavapestov requested a review from erg March 14, 2017 07:13
@karwa
Copy link
Contributor

karwa commented Mar 14, 2017

Some of the changes already got merged in. I've been maintaining a branch which contains the rest: https://github.com/karwa/swift

Basically the only things not yet merged are the CMake toolchain file and the pkg-config lookup in build-script-impl.

The idea was to refactor some of these options/data in to some kind of "cross-compilation bundle" which would not only be used by the build-script to build the compiler, but also understood by the compiler in order to cross-compile Swift executables later. The problem is that those bundles would need to contain the compiler's external dependencies, which are floating, platform-specific and not documented in a structured way for tooling. Basically, we would need to develop a kind of mini package manager. So since then I've been (casually) looking at using a C++ package manager instead, like https://github.com/ruslo/hunter or something.

@n8gray
Copy link

n8gray commented Jul 7, 2017

Does this PR still represent the state of cross-compiling in Swift? If so, is there any documentation on how to use it? I've been trying to build a cross-compiler to Raspberry Pi. I've grabbed @karwa's fork from https://github.com/karwa/swift and rebased his patches to master, put together a toolchain and a sysroot, and built with this command:

./utils/build-script -r --stdlib-deployment-targets linux-armv7 \
  -- \
  --cross-compile-sysroots="/SAPDevelop/src/swiftX/sysroot.armhf.debian.jessie/" \
  --cross-compile-toolchains="/SAPDevelop/src/swiftX/toolchain.gcc-arm-none-eabi-5_2-2015q4/"

Eventually I got this error:

CMake Error at cmake/modules/SwiftUtils.cmake:14 (message):
  Error! Unknown SDKs: LINUX
Call Stack (most recent call first):
  CMakeLists.txt:763 (precondition)

I'd be grateful for pointers from anybody who's made this work. Thanks!

@CodaFi
Copy link
Member

CodaFi commented Sep 19, 2017

Since @karwa is maintaining a more up-to-date fork, and this pull request has fallen behind the more comprehensive efforts to enable cross-compilation, I'm going to close it. Somebody should take a look at the state of the smaller pull requests that have been split off of this one and @karwa's branch and give us a more modern pull request.

@CodaFi CodaFi closed this Sep 19, 2017
kateinoigakukun pushed a commit to kateinoigakukun/swift that referenced this pull request Sep 7, 2020
Build both host and target toolchains in 5.3 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet