From 76ed118b5201af94e77de809fc4547c60b749e68 Mon Sep 17 00:00:00 2001 From: Yauheni Khnykin Date: Thu, 21 Dec 2023 18:21:40 +0100 Subject: [PATCH] Fixes various issues related framework imports in Swift - Fixes self import in final framework. - Adds preprocessor conditionds to disable an import. Signed-off-by: Yauheni Khnykin --- .../gluecodium/details/runGenerate.cmake | 4 + .../swift/TargetLinkFrameworks.cmake | 24 +++- .../CMakeLists.txt | 106 ++++++++++++++++++ .../cpp/CommonMainFooImpl.cpp | 29 +++++ .../cpp/MainBarImpl.cpp | 29 +++++ .../lime/common_main_foo.lime | 22 ++++ .../lime/main_bar.lime | 22 ++++ .../lime/common_main_foo.lime | 4 +- .../lime/common_main_foo.lime | 4 +- 9 files changed, 235 insertions(+), 9 deletions(-) create mode 100644 cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/CMakeLists.txt create mode 100644 cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/CommonMainFooImpl.cpp create mode 100644 cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/MainBarImpl.cpp create mode 100644 cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/common_main_foo.lime create mode 100644 cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/main_bar.lime diff --git a/cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake b/cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake index 0f93b046b9..c79b9effa6 100644 --- a/cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake +++ b/cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake @@ -267,7 +267,11 @@ function(_concatenate_swift_files) list(REMOVE_DUPLICATES GLUECODIUM_IMPORT_FRAMEWORKS) foreach(_import_framework IN LISTS GLUECODIUM_IMPORT_FRAMEWORKS) + string(TOUPPER "${_import_framework}" _framework_name_upper) + + string(APPEND _swift_source_header_main "#if !DONT_IMPORT_${_framework_name_upper}\n") string(APPEND _swift_source_header_main "import ${_import_framework}\n") + string(APPEND _swift_source_header_main "#endif\n") endforeach() foreach(_source_set ${GLUECODIUM_SOURCE_SETS}) diff --git a/cmake/modules/gluecodium/swift/TargetLinkFrameworks.cmake b/cmake/modules/gluecodium/swift/TargetLinkFrameworks.cmake index b90c95b7d8..724d0bb93c 100644 --- a/cmake/modules/gluecodium/swift/TargetLinkFrameworks.cmake +++ b/cmake/modules/gluecodium/swift/TargetLinkFrameworks.cmake @@ -84,9 +84,22 @@ function(gluecodium_target_link_frameworks _target) endfunction() -function(_gluecodium_target_link_framework target_dst target_framework) +function(_gluecodium_get_linked_static_targets_with_generated_sources out_variable _target) gluecodium_get_linked_targets_with_generated_sources(_targets_with_generated_sources - ${target_dst} ONLY_STATIC) + ${_target} ONLY_STATIC) + unset (_result) + foreach(_target_with_generated_sources IN LISTS _targets_with_generated_sources) + gluecodium_resolve_possible_alias(_target_with_generated_source_aliased + ${_target_with_generated_sources}) + list(APPEND _result ${_target_with_generated_source_aliased}) + endforeach() + set (${out_variable} "${_result}" PARENT_SCOPE) +endfunction() + +function(_gluecodium_target_link_framework target_dst target_framework) + _gluecodium_get_linked_static_targets_with_generated_sources(_targets_with_generated_sources ${target_dst}) + _gluecodium_get_linked_static_targets_with_generated_sources(_exclude_targets_with_generated_sources_alias + ${target_framework}) gluecodium_resolve_possible_alias(target_framework_aliased ${target_framework}) if(CMAKE_VERSION GREATER_EQUAL 3.12 AND NOT GLUECODIUM_DONT_USE_TARGET_GENEX_EVAL) @@ -99,9 +112,10 @@ function(_gluecodium_target_link_framework target_dst target_framework) endif() foreach(_target_with_generated_sources IN LISTS _targets_with_generated_sources) - gluecodium_resolve_possible_alias(_target_with_generated_source_aliased - ${_target_with_generated_sources}) - set_property(TARGET ${_target_with_generated_source_aliased} APPEND + if(_target_with_generated_sources IN_LIST _exclude_targets_with_generated_sources_alias) + continue() + endif() + set_property(TARGET ${_target_with_generated_sources} APPEND PROPERTY GLUECODIUM_IMPORT_FRAMEWORKS ${_framework_name}) endforeach() diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/CMakeLists.txt b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/CMakeLists.txt new file mode 100644 index 0000000000..42ed0f14ce --- /dev/null +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/CMakeLists.txt @@ -0,0 +1,106 @@ +cmake_minimum_required(VERSION 3.12) + +project(gluecodium.test) + +set(CMAKE_CXX_STANDARD 17) + + +# Following hierarchy ended up in Swift code which mistakenly. +# - imported framework1 in OBJECT BASE. +# - unconditionally imported framework1 and framework2 in OBJECT DEPENDANT1. +# +# OBJECT BASE +# / \ \ +# FRAMEWORK BASE1 \ FRAMEWORK BASE2 +# | \ | +# OBJECT DEPENDANT1 OBJECT DEPENDANT2 +# | \ | +# | \ | +# | \ | +# FRAMEWORK DEPENDANT1 FRAMEWORK DEPENDANT2 + +if(NOT APPLE OR NOT CMAKE_GENERATOR STREQUAL "Xcode") + return() +endif() + +enable_language(Swift) + +list(APPEND CMAKE_MODULE_PATH "${GLUECODIUM_CMAKE_DIR}/modules") + +include(${GLUECODIUM_CMAKE_TESTS_DIR}/utils/assert.cmake) +include(gluecodium/Gluecodium) +include(gluecodium/Swift) + +# OBJECT BASE +add_library(ObjectBase OBJECT) +gluecodium_generate(ObjectBase GENERATORS cpp swift) +gluecodium_target_lime_sources(ObjectBase + PUBLIC "${CMAKE_CURRENT_LIST_DIR}/lime/common_main_foo.lime") +set_target_properties(ObjectBase PROPERTIES GLUECODIUM_SWIFT_EXPOSE_INTERNALS ON) + +# FRAMEWORK BASE1 +add_library(FrameworkBase1 SHARED "${CMAKE_CURRENT_LIST_DIR}/cpp/CommonMainFooImpl.cpp") +target_link_libraries(FrameworkBase1 PUBLIC ObjectBase) +set(MACOSX_FRAMEWORK_NAME FrameworkBase1) +set_target_properties( + FrameworkBase1 + PROPERTIES FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER gluecodium.common.main.test + XCODE_ATTRIBUTE_DEFINES_MODULE YES) +gluecodium_target_swift_sources(FrameworkBase1 COLLECT_FROM_DEPENDENCIES + ADD_MODULE_MODULEMAP) + +# FRAMEWORK BASE2 +add_library(FrameworkBase2 SHARED EXCLUDE_FROM_ALL "${CMAKE_CURRENT_LIST_DIR}/cpp/CommonMainFooImpl.cpp") +target_link_libraries(FrameworkBase2 PUBLIC ObjectBase) +set(MACOSX_FRAMEWORK_NAME FrameworkBase2) +set_target_properties( + FrameworkBase2 + PROPERTIES FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER gluecodium.common.main.test + XCODE_ATTRIBUTE_DEFINES_MODULE YES) +gluecodium_target_swift_sources(FrameworkBase2 COLLECT_FROM_DEPENDENCIES + ADD_MODULE_MODULEMAP) + +# OBJECT DEPENDANT1 +add_library(ObjectDependant1 OBJECT) +gluecodium_generate(ObjectDependant1 GENERATORS cpp swift MAIN_ONLY) +gluecodium_target_lime_sources(ObjectDependant1 + PRIVATE "${CMAKE_CURRENT_LIST_DIR}/lime/main_bar.lime") +target_link_libraries(ObjectDependant1 PUBLIC FrameworkBase1) + +# OBJECT DEPENDANT2 +add_library(ObjectDependant2 OBJECT EXCLUDE_FROM_ALL) +gluecodium_generate(ObjectDependant2 GENERATORS cpp swift MAIN_ONLY) +gluecodium_target_lime_sources(ObjectDependant2 + PRIVATE "${CMAKE_CURRENT_LIST_DIR}/lime/main_bar.lime") +target_link_libraries(ObjectDependant2 PUBLIC ObjectBase FrameworkBase2) + +# FRAMEWORK DEPENDANT1 +add_library(FrameworkDependant1 SHARED "${CMAKE_CURRENT_LIST_DIR}/cpp/MainBarImpl.cpp" "${CMAKE_CURRENT_LIST_DIR}/cpp/MainBarImpl.cpp") +target_link_libraries(FrameworkDependant1 PRIVATE ObjectDependant1) +set(MACOSX_FRAMEWORK_NAME FrameworkDependant1) +set_target_properties( + FrameworkDependant1 + PROPERTIES FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER gluecodium.main.test + XCODE_ATTRIBUTE_DEFINES_MODULE YES + XCODE_ATTRIBUTE_SWIFT_ACTIVE_COMPILATION_CONDITIONS "DONT_IMPORT_FRAMEWORKBASE2") +gluecodium_target_swift_sources(FrameworkDependant1 COLLECT_FROM_DEPENDENCIES ADD_MODULE_MODULEMAP) +gluecodium_target_link_frameworks( + FrameworkDependant1 FRAMEWORKS FrameworkBase1 + COMMON_FRAMEWORK FrameworkBase1) + +# FRAMEWORK DEPENDANT2 +add_library(FrameworkDependant2 SHARED EXCLUDE_FROM_ALL "${CMAKE_CURRENT_LIST_DIR}/cpp/MainBarImpl.cpp" "${CMAKE_CURRENT_LIST_DIR}/cpp/MainBarImpl.cpp") +target_link_libraries(FrameworkDependant2 PRIVATE ObjectDependant1 ObjectDependant2) +set(MACOSX_FRAMEWORK_NAME FrameworkDependant2) +set_target_properties( + FrameworkDependant2 + PROPERTIES FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER gluecodium.main.test + XCODE_ATTRIBUTE_DEFINES_MODULE YES) +gluecodium_target_swift_sources(FrameworkDependant2 COLLECT_FROM_DEPENDENCIES ADD_MODULE_MODULEMAP) +gluecodium_target_link_frameworks( + FrameworkDependant2 FRAMEWORKS FrameworkBase2 + COMMON_FRAMEWORK FrameworkBase2) diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/CommonMainFooImpl.cpp b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/CommonMainFooImpl.cpp new file mode 100644 index 0000000000..23f5f42369 --- /dev/null +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/CommonMainFooImpl.cpp @@ -0,0 +1,29 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2023 HERE Europe B.V. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 +// License-Filename: LICENSE +// +// ------------------------------------------------------------------------------------------------- + +#include "unit/test/CommonMainFoo.h" + +namespace unit::test { +std::shared_ptr +CommonMainFoo::make() +{ + return nullptr; +} +} // namespace unit::test diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/MainBarImpl.cpp b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/MainBarImpl.cpp new file mode 100644 index 0000000000..24233f2b01 --- /dev/null +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/cpp/MainBarImpl.cpp @@ -0,0 +1,29 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2023 HERE Europe B.V. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 +// License-Filename: LICENSE +// +// ------------------------------------------------------------------------------------------------- + +#include "unit/test/MainBar.h" + +namespace unit::test { +std::shared_ptr +MainBar::make() +{ + return nullptr; +} +} // namespace unit::test diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/common_main_foo.lime b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/common_main_foo.lime new file mode 100644 index 0000000000..68c64a0111 --- /dev/null +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/common_main_foo.lime @@ -0,0 +1,22 @@ +# Copyright (C) 2023 HERE Europe B.V. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# License-Filename: LICENSE + +package unit.test + +class CommonMainFoo { + constructor make() +} diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/main_bar.lime b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/main_bar.lime new file mode 100644 index 0000000000..23b8ed2a50 --- /dev/null +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-and-dependent-object-libs-in-single-framework/lime/main_bar.lime @@ -0,0 +1,22 @@ +# Copyright (C) 2023 HERE Europe B.V. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# License-Filename: LICENSE + +package unit.test + +class MainBar { + constructor make() +} diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-shared-modules-dependent-on-object-libs/lime/common_main_foo.lime b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-shared-modules-dependent-on-object-libs/lime/common_main_foo.lime index ea98c61fe1..990671af78 100644 --- a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-shared-modules-dependent-on-object-libs/lime/common_main_foo.lime +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-shared-modules-dependent-on-object-libs/lime/common_main_foo.lime @@ -23,7 +23,7 @@ enum CommonBaseEnum { VALUE3 } -open class CommonMainFoo { +class CommonMainFoo { constructor make() # Converters for primitive types must be copied @@ -33,7 +33,7 @@ open class CommonMainFoo { fun get_enum() : CommonBaseEnum } -open interface CommonMainBase { +interface CommonMainBase { property common_base_id: Long property common_base_enum: CommonBaseEnum property common_base_type_foo: CommonMainFoo diff --git a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-single-shared-modules/lime/common_main_foo.lime b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-single-shared-modules/lime/common_main_foo.lime index f6ddf933e3..0060f8de96 100644 --- a/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-single-shared-modules/lime/common_main_foo.lime +++ b/cmake/tests/unit/gluecodium_target_link_frameworks/dependent-frameworks-with-single-shared-modules/lime/common_main_foo.lime @@ -17,12 +17,12 @@ package unit.test -open enum MainEnum { +enum MainEnum { VALUE1, VALUE2 } -open class CommonMainFoo { +class CommonMainFoo { constructor make() # Converters for primitive types must be copied