tools/corrosion/cmake/CorrosionGenerator.cmake
branchtransitional_engine
changeset 16067 d903f8d2395a
parent 16050 6a3dc15b78b9
--- a/tools/corrosion/cmake/CorrosionGenerator.cmake	Wed Sep 18 14:10:51 2024 +0200
+++ b/tools/corrosion/cmake/CorrosionGenerator.cmake	Wed Nov 20 21:37:47 2024 +0100
@@ -44,8 +44,14 @@
 # Add targets (crates) of one package
 function(_generator_add_package_targets)
     set(OPTIONS NO_LINKER_OVERRIDE)
-    set(ONE_VALUE_KEYWORDS WORKSPACE_MANIFEST_PATH PACKAGE_MANIFEST_PATH PACKAGE_NAME PACKAGE_VERSION TARGETS_JSON OUT_CREATED_TARGETS)
-    set(MULTI_VALUE_KEYWORDS CRATE_TYPES)
+    set(ONE_VALUE_KEYWORDS
+        WORKSPACE_MANIFEST_PATH
+        PACKAGE_MANIFEST_PATH
+        PACKAGE_NAME
+        PACKAGE_VERSION
+        TARGETS_JSON
+        OUT_CREATED_TARGETS)
+    set(MULTI_VALUE_KEYWORDS CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
     cmake_parse_arguments(PARSE_ARGV 0 GAPT "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
 
     if(DEFINED GAPT_UNPARSED_ARGUMENTS)
@@ -64,6 +70,10 @@
     set(targets "${GAPT_TARGETS_JSON}")
     set(out_created_targets "${GAPT_OUT_CREATED_TARGETS}")
     set(crate_types "${GAPT_CRATE_TYPES}")
+    if(DEFINED GAPT_OVERRIDE_CRATE_TYPE_ARGS)
+        list(GET GAPT_OVERRIDE_CRATE_TYPE_ARGS 0 override_crate_name_list_ref)
+        list(GET GAPT_OVERRIDE_CRATE_TYPE_ARGS 1 override_crate_types_list_ref)
+    endif()
 
     set(corrosion_targets "")
 
@@ -82,13 +92,26 @@
 
         math(EXPR target_kind_len-1 "${target_kind_len} - 1")
         set(kinds)
-        foreach(ix RANGE ${target_kind_len-1})
-            string(JSON kind GET "${target_kind}" ${ix})
-            if(NOT crate_types OR ${kind} IN_LIST crate_types)
-                list(APPEND kinds ${kind})
-            endif()
-        endforeach()
-
+        unset(override_package_crate_type)
+        # OVERRIDE_CRATE_TYPE is more specific than the CRATE_TYPES argument to corrosion_import_crate, and thus takes
+        # priority.
+        if(DEFINED GAPT_OVERRIDE_CRATE_TYPE_ARGS)
+            foreach(override_crate_name override_crate_types IN ZIP_LISTS ${override_crate_name_list_ref} ${override_crate_types_list_ref})
+                if("${override_crate_name}" STREQUAL "${target_name}")
+                    message(DEBUG "Forcing crate ${target_name} to crate-type(s): ${override_crate_types}.")
+                    # Convert to CMake list
+                    string(REPLACE "," ";" kinds "${override_crate_types}")
+                    break()
+                endif()
+            endforeach()
+        else()
+            foreach(ix RANGE ${target_kind_len-1})
+                string(JSON kind GET "${target_kind}" ${ix})
+                if(NOT crate_types OR ${kind} IN_LIST crate_types)
+                    list(APPEND kinds ${kind})
+                endif()
+            endforeach()
+        endif()
         if(TARGET "${target_name}"
             AND ("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds OR "bin" IN_LIST kinds)
             )
@@ -116,6 +139,8 @@
             set(shared_lib_byproduct "")
             set(pdb_byproduct "")
 
+            add_library(${target_name} INTERFACE)
+            _corrosion_initialize_properties(${target_name})
             _corrosion_add_library_target(
                 WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
                 TARGET_NAME "${target_name}"
@@ -152,15 +177,17 @@
             endif()
             if(pdb_byproduct)
                 _corrosion_copy_byproducts(
-                    ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}"
+                    ${target_name} "PDB_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
                 )
             endif()
             list(APPEND corrosion_targets ${target_name})
-            set_property(TARGET "${target_name}" PROPERTY INTERFACE_COR_CARGO_PACKAGE_NAME "${package_name}" )
+            set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
         # Note: "bin" is mutually exclusive with "staticlib/cdylib", since `bin`s are seperate crates from libraries.
         elseif("bin" IN_LIST kinds)
             set(bin_byproduct "")
             set(pdb_byproduct "")
+            add_executable(${target_name} IMPORTED GLOBAL)
+            _corrosion_initialize_properties(${target_name})
             _corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}"
                 "bin_byproduct" "pdb_byproduct"
             )
@@ -185,11 +212,11 @@
             )
             if(pdb_byproduct)
                 _corrosion_copy_byproducts(
-                        ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}"
+                        ${target_name} "PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
                 )
             endif()
             list(APPEND corrosion_targets ${target_name})
-            set_property(TARGET "${target_name}" PROPERTY INTERFACE_COR_CARGO_PACKAGE_NAME "${package_name}" )
+            set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
         else()
             # ignore other kinds (like examples, tests, build scripts, ...)
         endif()
@@ -209,7 +236,7 @@
 function(_generator_add_cargo_targets)
     set(options NO_LINKER_OVERRIDE)
     set(one_value_args MANIFEST_PATH IMPORTED_CRATES)
-    set(multi_value_args CRATES CRATE_TYPES)
+    set(multi_value_args CRATES CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
     cmake_parse_arguments(
         GGC
         "${options}"
@@ -221,6 +248,7 @@
 
     _corrosion_option_passthrough_helper(NO_LINKER_OVERRIDE GGC no_linker_override)
     _corrosion_arg_passthrough_helper(CRATE_TYPES GGC crate_types)
+    _corrosion_arg_passthrough_helper(OVERRIDE_CRATE_TYPE_ARGS GGC override_crate_types)
 
     _cargo_metadata(json "${GGC_MANIFEST_PATH}")
     string(JSON packages GET "${json}" "packages")
@@ -280,6 +308,7 @@
             OUT_CREATED_TARGETS curr_created_targets
             ${no_linker_override}
             ${crate_types}
+            ${override_crate_types}
         )
         list(APPEND created_targets "${curr_created_targets}")
     endforeach()
@@ -306,21 +335,4 @@
     if(GGC_IMPORTED_CRATES)
         set(${GGC_IMPORTED_CRATES} "${created_targets}" PARENT_SCOPE)
     endif()
-
-    foreach(target_name ${created_targets})
-        foreach(output_var RUNTIME_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY)
-            get_target_property(output_dir ${target_name} "${output_var}")
-            if (NOT output_dir AND DEFINED "CMAKE_${output_var}")
-                set_property(TARGET ${target_name} PROPERTY ${output_var} "${CMAKE_${output_var}}")
-            endif()
-
-            foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
-                string(TOUPPER "${config_type}" config_type_upper)
-                get_target_property(output_dir ${target_name} "${output_var}_${config_type_upper}")
-                if (NOT output_dir AND DEFINED "CMAKE_${output_var}_${config_type_upper}")
-                    set_property(TARGET ${target_name} PROPERTY "${output_var}_${config_type_upper}" "${CMAKE_${output_var}_${config_type_upper}}")
-                endif()
-            endforeach()
-        endforeach()
-    endforeach()
 endfunction()