Skip to content

Commit

Permalink
Fixes various issues related framework imports in Swift
Browse files Browse the repository at this point in the history
- Fixes self import in final framework.
- Adds preprocessor conditionds to disable an import.

Signed-off-by: Yauheni Khnykin <[email protected]>
  • Loading branch information
Hsilgos committed Dec 21, 2023
1 parent 836ca86 commit de874a1
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 9 deletions.
4 changes: 4 additions & 0 deletions cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
24 changes: 19 additions & 5 deletions cmake/modules/gluecodium/swift/TargetLinkFrameworks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 base 1
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 base 2
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)

# MAIN
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)

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)

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)


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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2020 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>
CommonMainFoo::make()
{
return nullptr;
}
} // namespace unit::test
Original file line number Diff line number Diff line change
@@ -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>
MainBar::make()
{
return nullptr;
}
} // namespace unit::test
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum CommonBaseEnum {
VALUE3
}

open class CommonMainFoo {
class CommonMainFoo {
constructor make()

# Converters for primitive types must be copied
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de874a1

Please sign in to comment.