@@ -607,6 +607,121 @@ function(add_swift_host_library name)
607607 endif ()
608608endfunction ()
609609
610+ # Add a module of libswift
611+ #
612+ # Creates a target to compile a module which is part of libswift.
613+ # Adds the module name to the global property "libswift_modules".
614+ #
615+ # This is a temporary workaround until it's possible to compile libswift with
616+ # cmake's builtin swift support.
617+ function (add_libswift_module module)
618+ cmake_parse_arguments (ALSM
619+ ""
620+ "DEPENDS"
621+ ""
622+ ${ARGN} )
623+ set (sources ${ALSM_UNPARSED_ARGUMENTS} )
624+ list (TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR} /" )
625+
626+ set (target_name "LibSwift${module} " )
627+
628+ # Add a target which depends on the actual compilation target, which
629+ # will be created in add_libswift.
630+ # This target is mainly used to add properties, like the list of source files.
631+ add_custom_target (
632+ ${target_name}
633+ SOURCES ${sources}
634+ COMMENT "libswift module ${module} " )
635+
636+ set_property (TARGET ${target_name} PROPERTY "module_name" ${module} )
637+ set_property (TARGET ${target_name} PROPERTY "module_depends" ${ALSM_DEPENDS} )
638+
639+ get_property (modules GLOBAL PROPERTY "libswift_modules" )
640+ set_property (GLOBAL PROPERTY "libswift_modules" ${modules} ${module} )
641+ endfunction ()
642+
643+ # Add source files to a libswift module.
644+ #
645+ # This is a temporary workaround until it's possible to compile libswift with
646+ # cmake's builtin swift support.
647+ function (libswift_sources module)
648+ cmake_parse_arguments (LSS
649+ ""
650+ ""
651+ ""
652+ ${ARGN} )
653+ set (sources ${LSS_UNPARSED_ARGUMENTS} )
654+ list (TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR} /" )
655+
656+ set (target_name "LibSwift${module} " )
657+ set_property (TARGET "LibSwift${module} " APPEND PROPERTY SOURCES ${sources} )
658+ endfunction ()
659+
660+ # Add the libswift library.
661+ #
662+ # Adds targets to compile all modules of libswift and a target for the
663+ # libswift library itself.
664+ #
665+ # This is a temporary workaround until it's possible to compile libswift with
666+ # cmake's builtin swift support.
667+ function (add_libswift name )
668+ if (CMAKE_BUILD_TYPE STREQUAL Debug)
669+ set (libswift_compile_options "-g" )
670+ else ()
671+ set (libswift_compile_options "-O" "-cross-module-optimization" )
672+ endif ()
673+
674+ set (build_dir ${CMAKE_CURRENT_BINARY_DIR} )
675+
676+ if (SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
677+ set (deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _DEPLOYMENT_VERSION}" )
678+ endif ()
679+ get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
680+ ${SWIFT_HOST_VARIANT_ARCH} "${deployment_version} " )
681+
682+ get_property (modules GLOBAL PROPERTY "libswift_modules" )
683+ foreach (module ${modules} )
684+
685+ set (module_target "LibSwift${module} " )
686+ get_target_property (module ${module_target} "module_name" )
687+ get_target_property (sources ${module_target} SOURCES )
688+ get_target_property (dependencies ${module_target} "module_depends" )
689+ if (dependencies)
690+ list (TRANSFORM dependencies PREPEND "LibSwift" )
691+ else ()
692+ set (dependencies "" )
693+ endif ()
694+
695+ set (module_obj_file "${build_dir} /${module} .o" )
696+ set (module_file "${build_dir} /${module} .swiftmodule" )
697+ set_property (TARGET ${module_target} PROPERTY "module_file" "${module_file} " )
698+
699+ set (all_obj_files ${all_obj_files} ${module_obj_file} )
700+
701+ # Compile the libswift module into an object file
702+ add_custom_command_target(dep_target OUTPUT ${module_obj_file}
703+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
704+ DEPENDS ${sources} ${dependencies}
705+ COMMAND ${CMAKE_Swift_COMPILER} "-c" "-o" ${module_obj_file}
706+ "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}"
707+ "-target" ${target}
708+ "-module-name" ${module} "-emit-module"
709+ "-emit-module-path" "${build_dir} /${module} .swiftmodule"
710+ "-parse-as-library" ${sources}
711+ "-wmo" ${libswift_compile_options}
712+ "-I" "${CMAKE_SOURCE_DIR} /include/swift"
713+ "-I" "${build_dir} "
714+ COMMENT "Building libswift module ${module} " )
715+
716+ add_dependencies (${module_target} ${dep_target} )
717+
718+ endforeach ()
719+
720+ # Create a static libswift library containing all module object files.
721+ add_library (${name} STATIC ${all_obj_files} )
722+ set_target_properties (${name} PROPERTIES LINKER_LANGUAGE CXX)
723+ endfunction ()
724+
610725macro (add_swift_tool_subdirectory name )
611726 add_llvm_subdirectory(SWIFT TOOL ${name} )
612727endmacro ()
@@ -616,7 +731,7 @@ macro(add_swift_lib_subdirectory name)
616731endmacro ()
617732
618733function (add_swift_host_tool executable)
619- set (options )
734+ set (options HAS_LIBSWIFT )
620735 set (single_parameter_options SWIFT_COMPONENT)
621736 set (multiple_parameter_options LLVM_LINK_COMPONENTS)
622737
@@ -668,6 +783,24 @@ function(add_swift_host_tool executable)
668783 get_filename_component (TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY )
669784 get_filename_component (TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR} /../lib/swift/macosx" ABSOLUTE )
670785 target_link_directories (${executable} PUBLIC ${TOOLCHAIN_LIB_DIR} )
786+
787+ if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
788+ # Workaround to make lldb happy: we have to explicitly add all libswift modules
789+ # to the linker command line.
790+ set (libswift_ast_path_flags "-Wl" )
791+ get_property (modules GLOBAL PROPERTY "libswift_modules" )
792+ foreach (module ${modules} )
793+ get_target_property (module_file "LibSwift${module} " "module_file" )
794+ string (APPEND libswift_ast_path_flags ",-add_ast_path,${module_file} " )
795+ endforeach ()
796+
797+ set_property (TARGET ${executable} APPEND_STRING PROPERTY
798+ LINK_FLAGS ${libswift_ast_path_flags} )
799+
800+ # Workaround for a linker crash related to autolinking: rdar://77839981
801+ set_property (TARGET ${executable} APPEND_STRING PROPERTY
802+ LINK_FLAGS " -lobjc " )
803+ endif ()
671804 endif ()
672805
673806 # Lists of rpaths that we are going to add to our executables.
@@ -687,6 +820,22 @@ function(add_swift_host_tool executable)
687820 set_target_properties (${executable} PROPERTIES
688821 BUILD_WITH_INSTALL_RPATH YES
689822 INSTALL_RPATH "${RPATH_LIST} " )
823+
824+ elseif (SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX" )
825+ if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
826+ # At build time and and run time, link against the swift libraries in the
827+ # installed host toolchain.
828+ get_filename_component (swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY )
829+ get_filename_component (swift_dir ${swift_bin_dir} DIRECTORY )
830+ set (host_lib_dir "${swift_dir} /lib/swift/linux" )
831+
832+ target_link_libraries (${executable} PRIVATE "swiftCore" )
833+
834+ target_link_directories (${executable} PRIVATE ${host_lib_dir} )
835+ set_target_properties (${executable} PROPERTIES
836+ BUILD_WITH_INSTALL_RPATH YES
837+ INSTALL_RPATH "${host_lib_dir} " )
838+ endif ()
690839 endif ()
691840
692841 llvm_update_compile_flags(${executable} )
0 commit comments