@@ -522,7 +522,7 @@ function(add_swift_host_library name)
522
522
523
523
add_library (${name} ${libkind} ${ASHL_SOURCES} )
524
524
525
- if (ASHL_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT )
525
+ if (ASHL_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE )
526
526
# Workaround for a linker crash related to autolinking: rdar://77839981
527
527
set_property (TARGET ${name} APPEND_STRING PROPERTY
528
528
LINK_FLAGS " -lobjc " )
@@ -724,8 +724,13 @@ endfunction()
724
724
# This is a temporary workaround until it's possible to compile libswift with
725
725
# cmake's builtin swift support.
726
726
function (add_libswift name )
727
+ cmake_parse_arguments (ALS
728
+ ""
729
+ "BOOTSTRAPPING;SWIFT_EXEC;DEPENDS"
730
+ ""
731
+ ${ARGN} )
732
+
727
733
set (libswift_compile_options
728
- "-target" "x86_64-apple-macosx10.15" # TODO: remove this once #38675 lands.
729
734
"-Xfrontend" "-validate-tbd-against-ir=none"
730
735
"-Xfrontend" "-enable-cxx-interop" )
731
736
@@ -735,10 +740,11 @@ function(add_libswift name)
735
740
list (APPEND libswift_compile_options "-O" "-cross-module-optimization" )
736
741
endif ()
737
742
738
- set (build_dir ${CMAKE_CURRENT_BINARY_DIR} )
743
+ get_bootstrapping_path (build_dir ${CMAKE_CURRENT_BINARY_DIR} " ${ALS_BOOTSTRAPPING} " )
739
744
740
745
if (SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
741
- set (deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _DEPLOYMENT_VERSION}" )
746
+ set (deployment_version "10.15" ) # TODO: once #38675 lands, replace this with
747
+ # set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
742
748
endif ()
743
749
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
744
750
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version} " )
@@ -750,10 +756,15 @@ function(add_libswift name)
750
756
get_target_property (module ${module_target} "module_name" )
751
757
get_target_property (sources ${module_target} SOURCES )
752
758
get_target_property (dependencies ${module_target} "module_depends" )
753
- if (dependencies)
754
- list (TRANSFORM dependencies PREPEND "LibSwift" )
755
- else ()
756
- set (dependencies "" )
759
+ set (deps, "" )
760
+ if (dependencies)
761
+ foreach (dep_module ${dependencies} )
762
+ if (DEFINED "${dep_module} _dep_target" )
763
+ list (APPEND deps "${${dep_module} _dep_target}" )
764
+ else ()
765
+ message (FATAL_ERROR "libswift module dependency ${module} -> ${dep_module} not found. Make sure to add modules in dependency order" )
766
+ endif ()
767
+ endforeach ()
757
768
endif ()
758
769
759
770
set (module_obj_file "${build_dir} /${module} .o" )
@@ -765,8 +776,8 @@ function(add_libswift name)
765
776
# Compile the libswift module into an object file
766
777
add_custom_command_target(dep_target OUTPUT ${module_obj_file}
767
778
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
768
- DEPENDS ${sources} ${dependencies }
769
- COMMAND ${CMAKE_Swift_COMPILER } "-c" "-o" ${module_obj_file}
779
+ DEPENDS ${sources} ${deps} ${ALS_DEPENDS }
780
+ COMMAND ${ALS_SWIFT_EXEC } "-c" "-o" ${module_obj_file}
770
781
"-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}"
771
782
"-target" ${target}
772
783
"-module-name" ${module} "-emit-module"
@@ -777,8 +788,7 @@ function(add_libswift name)
777
788
"-I" "${build_dir} "
778
789
COMMENT "Building libswift module ${module} " )
779
790
780
- add_dependencies (${module_target} ${dep_target} )
781
-
791
+ set ("${module} _dep_target" ${dep_target} )
782
792
endforeach ()
783
793
784
794
# Create a static libswift library containing all module object files.
@@ -794,9 +804,19 @@ macro(add_swift_lib_subdirectory name)
794
804
add_llvm_subdirectory(SWIFT LIB ${name} )
795
805
endmacro ()
796
806
807
+ function (_link_built_compatibility_libs executable)
808
+ set (platform ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR})
809
+ target_link_directories (${executable} PRIVATE
810
+ ${SWIFTLIB_DIR} /${platform} )
811
+ add_dependencies (${executable}
812
+ "swiftCompatibility50-${platform} "
813
+ "swiftCompatibility51-${platform} "
814
+ "swiftCompatibilityDynamicReplacements-${platform} " )
815
+ endfunction ()
816
+
797
817
function (add_swift_host_tool executable)
798
818
set (options HAS_LIBSWIFT)
799
- set (single_parameter_options SWIFT_COMPONENT)
819
+ set (single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING )
800
820
set (multiple_parameter_options LLVM_LINK_COMPONENTS)
801
821
802
822
cmake_parse_arguments (ASHT
@@ -835,76 +855,119 @@ function(add_swift_host_tool executable)
835
855
add_dependencies (${executable} ${LLVM_COMMON_DEPENDS} )
836
856
endif ()
837
857
858
+ if (NOT ${ASHT_BOOTSTRAPPING} STREQUAL "" )
859
+ # Strip the "-bootstrapping<n>" suffix from the target name to get the base
860
+ # executable name.
861
+ string (REGEX REPLACE "-bootstrapping.*" "" executable_filename ${executable} )
862
+ set_target_properties (${executable}
863
+ PROPERTIES OUTPUT_NAME ${executable_filename} )
864
+ endif ()
865
+
838
866
set_target_properties (${executable} PROPERTIES
839
867
FOLDER "Swift executables" )
840
868
if (SWIFT_PARALLEL_LINK_JOBS)
841
869
set_target_properties (${executable} PROPERTIES
842
870
JOB_POOL_LINK swift_link_job_pool)
843
871
endif ()
844
872
if (${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
873
+
874
+ # Lists of rpaths that we are going to add to our executables.
875
+ #
876
+ # Please add each rpath separately below to the list, explaining why you are
877
+ # adding it.
878
+ set (RPATH_LIST)
879
+
845
880
# If we found a swift compiler and are going to use swift code in swift
846
881
# host side tools but link with clang, add the appropriate -L paths so we
847
882
# find all of the necessary swift libraries on Darwin.
848
- if (CMAKE_Swift_COMPILER)
849
- # Add in the toolchain directory so we can grab compatibility libraries
850
- get_filename_component (TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY )
851
- get_filename_component (TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR} /../lib/swift/macosx" ABSOLUTE )
852
- target_link_directories (${executable} PUBLIC ${TOOLCHAIN_LIB_DIR} )
883
+ if (ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE)
884
+
885
+ if (LIBSWIFT_BUILD_MODE STREQUAL "HOSTTOOLS" )
886
+ # Add in the toolchain directory so we can grab compatibility libraries
887
+ get_filename_component (TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_LIBSWIFT} DIRECTORY )
888
+ get_filename_component (TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR} /../lib/swift/macosx" ABSOLUTE )
889
+ target_link_directories (${executable} PUBLIC ${TOOLCHAIN_LIB_DIR} )
890
+
891
+ # Add in the SDK directory for the host platform.
892
+ target_link_directories (${executable} PRIVATE
893
+ ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}/usr/lib/swift)
894
+
895
+ # Include the abi stable system stdlib in our rpath.
896
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
897
+
898
+ elseif (LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS" )
899
+ # Pick up the built libswiftCompatibility<n>.a libraries
900
+ _link_built_compatibility_libs(${executable} )
901
+
902
+ # Add in the SDK directory for the host platform.
903
+ target_link_directories (${executable} PRIVATE
904
+ ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}/usr/lib/swift)
853
905
854
- # Add in the SDK directory for the host platform and add an rpath.
906
+ # Include the abi stable system stdlib in our rpath.
907
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
908
+
909
+ elseif (LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING" )
910
+ # Pick up the built libswiftCompatibility<n>.a libraries
911
+ _link_built_compatibility_libs(${executable} )
912
+
913
+ # At build time link against the built swift libraries from the
914
+ # previous bootstrapping stage.
915
+ get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping} " )
916
+ target_link_directories (${executable} PRIVATE ${bs_lib_dir} )
917
+
918
+ # At runtime link against the built swift libraries from the current
919
+ # bootstrapping stage.
920
+ list (APPEND RPATH_LIST "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
921
+ else ()
922
+ message (FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE} '" )
923
+ endif ()
924
+
925
+ # Workaround to make lldb happy: we have to explicitly add all libswift modules
926
+ # to the linker command line.
927
+ set (libswift_ast_path_flags "-Wl" )
928
+ get_property (modules GLOBAL PROPERTY "libswift_modules" )
929
+ foreach (module ${modules} )
930
+ get_target_property (module_file "LibSwift${module} " "module_file" )
931
+ string (APPEND libswift_ast_path_flags ",-add_ast_path,${module_file} " )
932
+ endforeach ()
933
+
934
+ set_property (TARGET ${executable} APPEND_STRING PROPERTY
935
+ LINK_FLAGS ${libswift_ast_path_flags} )
936
+
937
+ # Workaround for a linker crash related to autolinking: rdar://77839981
938
+ set_property (TARGET ${executable} APPEND_STRING PROPERTY
939
+ LINK_FLAGS " -lobjc " )
940
+
941
+ else () # ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE
942
+
943
+ # TODO: do we really need this? Do any tools which don't link libswift include other swift code?
944
+
945
+ # Add in the SDK directory for the host platform.
855
946
#
856
947
# NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to
857
948
# ensure that we first find libraries from the toolchain, rather than from
858
- # the SDK. The reason why this is important is that when we perform a
859
- # stage2 build, this path is into the stage1 build. This is not a pure SDK
860
- # and also contains compatibility libraries. We need to make sure that the
861
- # compiler sees the actual toolchain's compatibility libraries first
862
- # before the just built compability libraries or build errors occur.
949
+ # the SDK.
863
950
target_link_directories (${executable} PRIVATE
864
951
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}/usr/lib/swift)
865
952
866
- if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
867
- # Workaround to make lldb happy: we have to explicitly add all libswift modules
868
- # to the linker command line.
869
- set (libswift_ast_path_flags "-Wl" )
870
- get_property (modules GLOBAL PROPERTY "libswift_modules" )
871
- foreach (module ${modules} )
872
- get_target_property (module_file "LibSwift${module} " "module_file" )
873
- string (APPEND libswift_ast_path_flags ",-add_ast_path,${module_file} " )
874
- endforeach ()
875
-
876
- set_property (TARGET ${executable} APPEND_STRING PROPERTY
877
- LINK_FLAGS ${libswift_ast_path_flags} )
878
-
879
- # Workaround for a linker crash related to autolinking: rdar://77839981
880
- set_property (TARGET ${executable} APPEND_STRING PROPERTY
881
- LINK_FLAGS " -lobjc " )
882
- endif ()
883
- endif ()
953
+ # We also want to be able to find libraries from the base toolchain
954
+ # directory. This is so swiftc can rely on its own host side dylibs that may
955
+ # contain swift content.
956
+ list (APPEND RPATH_LIST "@executable_path/../lib" )
884
957
885
- # Lists of rpaths that we are going to add to our executables.
886
- #
887
- # Please add each rpath separately below to the list, explaining why you are
888
- # adding it.
889
- set (RPATH_LIST)
890
-
891
- # We also want to be able to find libraries from the base toolchain
892
- # directory. This is so swiftc can rely on its own host side dylibs that may
893
- # contain swift content.
894
- list (APPEND RPATH_LIST "@executable_path/../lib" )
895
-
896
- # Also include the abi stable system stdlib in our rpath.
897
- list (APPEND RPATH_LIST "/usr/lib/swift" )
958
+ # Also include the abi stable system stdlib in our rpath.
959
+ list (APPEND RPATH_LIST "/usr/lib/swift" )
960
+ endif ()
898
961
899
962
set_target_properties (${executable} PROPERTIES
900
963
BUILD_WITH_INSTALL_RPATH YES
901
964
INSTALL_RPATH "${RPATH_LIST} " )
902
965
903
- elseif (SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX" )
904
- if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT )
966
+ elseif (SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX" AND ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE )
967
+ if (LIBSWIFT_BUILD_MODE STREQUAL "HOSTTOOLS" )
905
968
# At build time and and run time, link against the swift libraries in the
906
969
# installed host toolchain.
907
- get_filename_component (swift_bin_dir ${CMAKE_Swift_COMPILER } DIRECTORY )
970
+ get_filename_component (swift_bin_dir ${SWIFT_EXEC_FOR_LIBSWIFT } DIRECTORY )
908
971
get_filename_component (swift_dir ${swift_bin_dir} DIRECTORY )
909
972
set (host_lib_dir "${swift_dir} /lib/swift/linux" )
910
973
@@ -914,14 +977,36 @@ function(add_swift_host_tool executable)
914
977
set_target_properties (${executable} PROPERTIES
915
978
BUILD_WITH_INSTALL_RPATH YES
916
979
INSTALL_RPATH "${host_lib_dir} " )
980
+
981
+ elseif (LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING" )
982
+ # At build time link against the built swift libraries from the
983
+ # previous bootstrapping stage.
984
+ get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping} " )
985
+ target_link_directories (${executable} PRIVATE ${bs_lib_dir} )
986
+
987
+ # At runtime link against the built swift libraries from the current
988
+ # bootstrapping stage.
989
+ set_target_properties (${executable} PROPERTIES
990
+ BUILD_WITH_INSTALL_RPATH YES
991
+ INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SDK_LINUX_LIB_SUBDIR} " )
992
+
993
+ elseif (LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS" )
994
+ message (FATAL_ERROR "LIBSWIFT_BUILD_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux" )
995
+ else ()
996
+ message (FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE} '" )
917
997
endif ()
918
998
endif ()
919
999
920
1000
llvm_update_compile_flags(${executable} )
921
1001
swift_common_llvm_config(${executable} ${ASHT_LLVM_LINK_COMPONENTS} )
1002
+
1003
+ get_bootstrapping_path(out_bin_dir
1004
+ ${SWIFT_RUNTIME_OUTPUT_INTDIR} "${ASHT_BOOTSTRAPPING} " )
1005
+ get_bootstrapping_path(out_lib_dir
1006
+ ${SWIFT_LIBRARY_OUTPUT_INTDIR} "${ASHT_BOOTSTRAPPING} " )
922
1007
set_output_directory(${executable}
923
- BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR }
924
- LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR } )
1008
+ BINARY_DIR ${out_bin_dir }
1009
+ LIBRARY_DIR ${out_lib_dir } )
925
1010
926
1011
if (SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
927
1012
swift_windows_include_for_arch(${SWIFT_HOST_VARIANT_ARCH}
@@ -940,13 +1025,15 @@ function(add_swift_host_tool executable)
940
1025
endif ()
941
1026
endif ()
942
1027
943
- add_dependencies (${ASHT_SWIFT_COMPONENT} ${executable} )
944
- swift_install_in_component(TARGETS ${executable}
945
- RUNTIME
946
- DESTINATION bin
947
- COMPONENT ${ASHT_SWIFT_COMPONENT} )
1028
+ if (NOT ${ASHT_SWIFT_COMPONENT} STREQUAL "no_component" )
1029
+ add_dependencies (${ASHT_SWIFT_COMPONENT} ${executable} )
1030
+ swift_install_in_component(TARGETS ${executable}
1031
+ RUNTIME
1032
+ DESTINATION bin
1033
+ COMPONENT ${ASHT_SWIFT_COMPONENT} )
948
1034
949
- swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
1035
+ swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
1036
+ endif ()
950
1037
951
1038
if (NOT is_installing)
952
1039
set_property (GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${executable} )
0 commit comments