tools/corrosion/cmake/CorrosionGenerator.cmake
branchtransitional_engine
changeset 16038 d903f8d2395a
parent 16021 6a3dc15b78b9
equal deleted inserted replaced
16036:7b8d96fc8799 16038:d903f8d2395a
    42 endfunction()
    42 endfunction()
    43 
    43 
    44 # Add targets (crates) of one package
    44 # Add targets (crates) of one package
    45 function(_generator_add_package_targets)
    45 function(_generator_add_package_targets)
    46     set(OPTIONS NO_LINKER_OVERRIDE)
    46     set(OPTIONS NO_LINKER_OVERRIDE)
    47     set(ONE_VALUE_KEYWORDS WORKSPACE_MANIFEST_PATH PACKAGE_MANIFEST_PATH PACKAGE_NAME PACKAGE_VERSION TARGETS_JSON OUT_CREATED_TARGETS)
    47     set(ONE_VALUE_KEYWORDS
    48     set(MULTI_VALUE_KEYWORDS CRATE_TYPES)
    48         WORKSPACE_MANIFEST_PATH
       
    49         PACKAGE_MANIFEST_PATH
       
    50         PACKAGE_NAME
       
    51         PACKAGE_VERSION
       
    52         TARGETS_JSON
       
    53         OUT_CREATED_TARGETS)
       
    54     set(MULTI_VALUE_KEYWORDS CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
    49     cmake_parse_arguments(PARSE_ARGV 0 GAPT "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
    55     cmake_parse_arguments(PARSE_ARGV 0 GAPT "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
    50 
    56 
    51     if(DEFINED GAPT_UNPARSED_ARGUMENTS)
    57     if(DEFINED GAPT_UNPARSED_ARGUMENTS)
    52         message(FATAL_ERROR "Internal error - unexpected arguments: ${GAPT_UNPARSED_ARGUMENTS}")
    58         message(FATAL_ERROR "Internal error - unexpected arguments: ${GAPT_UNPARSED_ARGUMENTS}")
    53     elseif(DEFINED GAPT_KEYWORDS_MISSING_VALUES)
    59     elseif(DEFINED GAPT_KEYWORDS_MISSING_VALUES)
    62     set(package_name "${GAPT_PACKAGE_NAME}")
    68     set(package_name "${GAPT_PACKAGE_NAME}")
    63     set(package_version "${GAPT_PACKAGE_VERSION}")
    69     set(package_version "${GAPT_PACKAGE_VERSION}")
    64     set(targets "${GAPT_TARGETS_JSON}")
    70     set(targets "${GAPT_TARGETS_JSON}")
    65     set(out_created_targets "${GAPT_OUT_CREATED_TARGETS}")
    71     set(out_created_targets "${GAPT_OUT_CREATED_TARGETS}")
    66     set(crate_types "${GAPT_CRATE_TYPES}")
    72     set(crate_types "${GAPT_CRATE_TYPES}")
       
    73     if(DEFINED GAPT_OVERRIDE_CRATE_TYPE_ARGS)
       
    74         list(GET GAPT_OVERRIDE_CRATE_TYPE_ARGS 0 override_crate_name_list_ref)
       
    75         list(GET GAPT_OVERRIDE_CRATE_TYPE_ARGS 1 override_crate_types_list_ref)
       
    76     endif()
    67 
    77 
    68     set(corrosion_targets "")
    78     set(corrosion_targets "")
    69 
    79 
    70     file(TO_CMAKE_PATH "${package_manifest_path}" manifest_path)
    80     file(TO_CMAKE_PATH "${package_manifest_path}" manifest_path)
    71 
    81 
    80         string(JSON target_kind GET "${target}" "kind")
    90         string(JSON target_kind GET "${target}" "kind")
    81         string(JSON target_kind_len LENGTH "${target_kind}")
    91         string(JSON target_kind_len LENGTH "${target_kind}")
    82 
    92 
    83         math(EXPR target_kind_len-1 "${target_kind_len} - 1")
    93         math(EXPR target_kind_len-1 "${target_kind_len} - 1")
    84         set(kinds)
    94         set(kinds)
    85         foreach(ix RANGE ${target_kind_len-1})
    95         unset(override_package_crate_type)
    86             string(JSON kind GET "${target_kind}" ${ix})
    96         # OVERRIDE_CRATE_TYPE is more specific than the CRATE_TYPES argument to corrosion_import_crate, and thus takes
    87             if(NOT crate_types OR ${kind} IN_LIST crate_types)
    97         # priority.
    88                 list(APPEND kinds ${kind})
    98         if(DEFINED GAPT_OVERRIDE_CRATE_TYPE_ARGS)
    89             endif()
    99             foreach(override_crate_name override_crate_types IN ZIP_LISTS ${override_crate_name_list_ref} ${override_crate_types_list_ref})
    90         endforeach()
   100                 if("${override_crate_name}" STREQUAL "${target_name}")
    91 
   101                     message(DEBUG "Forcing crate ${target_name} to crate-type(s): ${override_crate_types}.")
       
   102                     # Convert to CMake list
       
   103                     string(REPLACE "," ";" kinds "${override_crate_types}")
       
   104                     break()
       
   105                 endif()
       
   106             endforeach()
       
   107         else()
       
   108             foreach(ix RANGE ${target_kind_len-1})
       
   109                 string(JSON kind GET "${target_kind}" ${ix})
       
   110                 if(NOT crate_types OR ${kind} IN_LIST crate_types)
       
   111                     list(APPEND kinds ${kind})
       
   112                 endif()
       
   113             endforeach()
       
   114         endif()
    92         if(TARGET "${target_name}"
   115         if(TARGET "${target_name}"
    93             AND ("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds OR "bin" IN_LIST kinds)
   116             AND ("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds OR "bin" IN_LIST kinds)
    94             )
   117             )
    95             message(WARNING "Failed to import Rust crate ${target_name} (kind: `${target_kind}`) because a target "
   118             message(WARNING "Failed to import Rust crate ${target_name} (kind: `${target_kind}`) because a target "
    96                 "with the same name already exists. Skipping this target.\n"
   119                 "with the same name already exists. Skipping this target.\n"
   114 
   137 
   115             set(archive_byproducts "")
   138             set(archive_byproducts "")
   116             set(shared_lib_byproduct "")
   139             set(shared_lib_byproduct "")
   117             set(pdb_byproduct "")
   140             set(pdb_byproduct "")
   118 
   141 
       
   142             add_library(${target_name} INTERFACE)
       
   143             _corrosion_initialize_properties(${target_name})
   119             _corrosion_add_library_target(
   144             _corrosion_add_library_target(
   120                 WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
   145                 WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
   121                 TARGET_NAME "${target_name}"
   146                 TARGET_NAME "${target_name}"
   122                 LIB_KINDS ${kinds}
   147                 LIB_KINDS ${kinds}
   123                 OUT_ARCHIVE_OUTPUT_BYPRODUCTS archive_byproducts
   148                 OUT_ARCHIVE_OUTPUT_BYPRODUCTS archive_byproducts
   150                     ${target_name} LIBRARY_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${shared_lib_byproduct}"
   175                     ${target_name} LIBRARY_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${shared_lib_byproduct}"
   151                 )
   176                 )
   152             endif()
   177             endif()
   153             if(pdb_byproduct)
   178             if(pdb_byproduct)
   154                 _corrosion_copy_byproducts(
   179                 _corrosion_copy_byproducts(
   155                     ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}"
   180                     ${target_name} "PDB_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
   156                 )
   181                 )
   157             endif()
   182             endif()
   158             list(APPEND corrosion_targets ${target_name})
   183             list(APPEND corrosion_targets ${target_name})
   159             set_property(TARGET "${target_name}" PROPERTY INTERFACE_COR_CARGO_PACKAGE_NAME "${package_name}" )
   184             set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
   160         # Note: "bin" is mutually exclusive with "staticlib/cdylib", since `bin`s are seperate crates from libraries.
   185         # Note: "bin" is mutually exclusive with "staticlib/cdylib", since `bin`s are seperate crates from libraries.
   161         elseif("bin" IN_LIST kinds)
   186         elseif("bin" IN_LIST kinds)
   162             set(bin_byproduct "")
   187             set(bin_byproduct "")
   163             set(pdb_byproduct "")
   188             set(pdb_byproduct "")
       
   189             add_executable(${target_name} IMPORTED GLOBAL)
       
   190             _corrosion_initialize_properties(${target_name})
   164             _corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}"
   191             _corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}"
   165                 "bin_byproduct" "pdb_byproduct"
   192                 "bin_byproduct" "pdb_byproduct"
   166             )
   193             )
   167 
   194 
   168             set(byproducts "")
   195             set(byproducts "")
   183             _corrosion_copy_byproducts(
   210             _corrosion_copy_byproducts(
   184                     ${target_name} RUNTIME_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${bin_byproduct}"
   211                     ${target_name} RUNTIME_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${bin_byproduct}"
   185             )
   212             )
   186             if(pdb_byproduct)
   213             if(pdb_byproduct)
   187                 _corrosion_copy_byproducts(
   214                 _corrosion_copy_byproducts(
   188                         ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}"
   215                         ${target_name} "PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
   189                 )
   216                 )
   190             endif()
   217             endif()
   191             list(APPEND corrosion_targets ${target_name})
   218             list(APPEND corrosion_targets ${target_name})
   192             set_property(TARGET "${target_name}" PROPERTY INTERFACE_COR_CARGO_PACKAGE_NAME "${package_name}" )
   219             set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
   193         else()
   220         else()
   194             # ignore other kinds (like examples, tests, build scripts, ...)
   221             # ignore other kinds (like examples, tests, build scripts, ...)
   195         endif()
   222         endif()
   196     endforeach()
   223     endforeach()
   197 
   224 
   207 # Add all cargo targets defined in the packages defined in the Cargo.toml manifest at
   234 # Add all cargo targets defined in the packages defined in the Cargo.toml manifest at
   208 # `MANIFEST_PATH`.
   235 # `MANIFEST_PATH`.
   209 function(_generator_add_cargo_targets)
   236 function(_generator_add_cargo_targets)
   210     set(options NO_LINKER_OVERRIDE)
   237     set(options NO_LINKER_OVERRIDE)
   211     set(one_value_args MANIFEST_PATH IMPORTED_CRATES)
   238     set(one_value_args MANIFEST_PATH IMPORTED_CRATES)
   212     set(multi_value_args CRATES CRATE_TYPES)
   239     set(multi_value_args CRATES CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
   213     cmake_parse_arguments(
   240     cmake_parse_arguments(
   214         GGC
   241         GGC
   215         "${options}"
   242         "${options}"
   216         "${one_value_args}"
   243         "${one_value_args}"
   217         "${multi_value_args}"
   244         "${multi_value_args}"
   219     )
   246     )
   220     list(APPEND CMAKE_MESSAGE_CONTEXT "_add_cargo_targets")
   247     list(APPEND CMAKE_MESSAGE_CONTEXT "_add_cargo_targets")
   221 
   248 
   222     _corrosion_option_passthrough_helper(NO_LINKER_OVERRIDE GGC no_linker_override)
   249     _corrosion_option_passthrough_helper(NO_LINKER_OVERRIDE GGC no_linker_override)
   223     _corrosion_arg_passthrough_helper(CRATE_TYPES GGC crate_types)
   250     _corrosion_arg_passthrough_helper(CRATE_TYPES GGC crate_types)
       
   251     _corrosion_arg_passthrough_helper(OVERRIDE_CRATE_TYPE_ARGS GGC override_crate_types)
   224 
   252 
   225     _cargo_metadata(json "${GGC_MANIFEST_PATH}")
   253     _cargo_metadata(json "${GGC_MANIFEST_PATH}")
   226     string(JSON packages GET "${json}" "packages")
   254     string(JSON packages GET "${json}" "packages")
   227     string(JSON workspace_members GET "${json}" "workspace_members")
   255     string(JSON workspace_members GET "${json}" "workspace_members")
   228 
   256 
   278             PACKAGE_VERSION "${pkg_version}"
   306             PACKAGE_VERSION "${pkg_version}"
   279             TARGETS_JSON "${targets}"
   307             TARGETS_JSON "${targets}"
   280             OUT_CREATED_TARGETS curr_created_targets
   308             OUT_CREATED_TARGETS curr_created_targets
   281             ${no_linker_override}
   309             ${no_linker_override}
   282             ${crate_types}
   310             ${crate_types}
       
   311             ${override_crate_types}
   283         )
   312         )
   284         list(APPEND created_targets "${curr_created_targets}")
   313         list(APPEND created_targets "${curr_created_targets}")
   285     endforeach()
   314     endforeach()
   286 
   315 
   287     if(NOT created_targets)
   316     if(NOT created_targets)
   304     endif()
   333     endif()
   305 
   334 
   306     if(GGC_IMPORTED_CRATES)
   335     if(GGC_IMPORTED_CRATES)
   307         set(${GGC_IMPORTED_CRATES} "${created_targets}" PARENT_SCOPE)
   336         set(${GGC_IMPORTED_CRATES} "${created_targets}" PARENT_SCOPE)
   308     endif()
   337     endif()
   309 
       
   310     foreach(target_name ${created_targets})
       
   311         foreach(output_var RUNTIME_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY)
       
   312             get_target_property(output_dir ${target_name} "${output_var}")
       
   313             if (NOT output_dir AND DEFINED "CMAKE_${output_var}")
       
   314                 set_property(TARGET ${target_name} PROPERTY ${output_var} "${CMAKE_${output_var}}")
       
   315             endif()
       
   316 
       
   317             foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
       
   318                 string(TOUPPER "${config_type}" config_type_upper)
       
   319                 get_target_property(output_dir ${target_name} "${output_var}_${config_type_upper}")
       
   320                 if (NOT output_dir AND DEFINED "CMAKE_${output_var}_${config_type_upper}")
       
   321                     set_property(TARGET ${target_name} PROPERTY "${output_var}_${config_type_upper}" "${CMAKE_${output_var}_${config_type_upper}}")
       
   322                 endif()
       
   323             endforeach()
       
   324         endforeach()
       
   325     endforeach()
       
   326 endfunction()
   338 endfunction()