Skip to content

Commit

Permalink
cmake: replace native library name by DOTNET_NATIVE_LIBRARY
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Jan 29, 2024
1 parent c2cd57e commit bd8b83f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Bar/dotnet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set_property(SOURCE bar.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE bar.i PROPERTY COMPILE_OPTIONS
-namespace ${DOTNET_PROJECT}.Bar
-dllimport mizux-dotnetnative-native)
-dllimport ${DOTNET_NATIVE_LIBRARY})
swig_add_library(dotnet_Bar
TYPE OBJECT
LANGUAGE csharp
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

option(CMAKE_EXPORT_COMPILE_COMMANDS "Export compile command" TRUE)

set(COMPANY_NAME Mizux)
project(DotnetNative VERSION 1.0 LANGUAGES CXX)
set(PROJECT_NAMESPACE dn)
message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}")
Expand Down
2 changes: 1 addition & 1 deletion Foo/dotnet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set_property(SOURCE foo.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE foo.i PROPERTY COMPILE_OPTIONS
-namespace ${DOTNET_PROJECT}.Foo
-dllimport mizux-dotnetnative-native)
-dllimport ${DOTNET_NATIVE_LIBRARY})
swig_add_library(dotnet_Foo
TYPE OBJECT
LANGUAGE csharp
Expand Down
2 changes: 1 addition & 1 deletion FooBar/dotnet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set_property(SOURCE foobar.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE foobar.i PROPERTY COMPILE_OPTIONS
-namespace ${DOTNET_PROJECT}.FooBar
-dllimport mizux-dotnetnative-native)
-dllimport ${DOTNET_NATIVE_LIBRARY})
swig_add_library(dotnet_FooBar
TYPE OBJECT
LANGUAGE csharp
Expand Down
36 changes: 19 additions & 17 deletions cmake/dotnet.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ else()
endif()

# Needed by dotnet/CMakeLists.txt
set(DOTNET_PACKAGE Mizux.DotnetNative)
set(DOTNET_PACKAGES_DIR "${PROJECT_BINARY_DIR}/dotnet/packages")
set(DOTNET_PROJECT ${COMPANY_NAME}.${PROJECT_NAME})
message(STATUS ".Net project: ${DOTNET_PROJECT}")
set(DOTNET_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_PROJECT})
message(STATUS ".Net project build path: ${DOTNET_PROJECT_DIR}")

# Runtime IDentifier
# see: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
Expand All @@ -42,11 +44,13 @@ else()
endif()
message(STATUS ".Net RID: ${DOTNET_RID}")

set(DOTNET_NATIVE_PROJECT ${DOTNET_PACKAGE}.runtime.${DOTNET_RID})
set(DOTNET_NATIVE_PROJECT ${DOTNET_PROJECT}.runtime.${DOTNET_RID})
message(STATUS ".Net runtime project: ${DOTNET_NATIVE_PROJECT}")
set(DOTNET_NATIVE_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_NATIVE_PROJECT})
message(STATUS ".Net runtime project build path: ${DOTNET_NATIVE_PROJECT_DIR}")

set(DOTNET_PACKAGES_DIR "${PROJECT_BINARY_DIR}/dotnet/packages")

# Targeted Framework Moniker
# see: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
# see: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
Expand Down Expand Up @@ -91,29 +95,27 @@ else()
string(CONCAT DOTNET_TFM "<TargetFramework>" "${DOTNET_TFM}" "</TargetFramework>")
endif()

set(DOTNET_PROJECT ${DOTNET_PACKAGE})
message(STATUS ".Net project: ${DOTNET_PROJECT}")
set(DOTNET_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_PROJECT})
message(STATUS ".Net project build path: ${DOTNET_PROJECT_DIR}")

# Create the native library
add_library(mizux-dotnetnative-native SHARED "")
set_target_properties(mizux-dotnetnative-native PROPERTIES
string(TOLOWER "${COMPANY_NAME}_${PROJECT_NAME}_native" DOTNET_NATIVE_LIBRARY)
message(STATUS ".Net runtime library: ${DOTNET_NATIVE_LIBRARY}")

add_library(${DOTNET_NATIVE_LIBRARY} SHARED "")
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
PREFIX ""
POSITION_INDEPENDENT_CODE ON)
# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(mizux-dotnetnative-native PROPERTIES
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
INSTALL_RPATH "@loader_path")
# Xcode fails to build if library doesn't contains at least one source file.
if(XCODE)
file(GENERATE
OUTPUT ${PROJECT_BINARY_DIR}/mizux-dotnetnative-native/version.cpp
OUTPUT ${PROJECT_BINARY_DIR}/${DOTNET_NATIVE_LIBRARY}/version.cpp
CONTENT "namespace {char* version = \"${PROJECT_VERSION}\";}")
target_sources(mizux-dotnetnative-native PRIVATE ${PROJECT_BINARY_DIR}/mizux-dotnetnative-native/version.cpp)
target_sources(${DOTNET_NATIVE_LIBRARY} PRIVATE ${PROJECT_BINARY_DIR}/${DOTNET_NATIVE_LIBRARY}/version.cpp)
endif()
elseif(UNIX)
set_target_properties(mizux-dotnetnative-native PROPERTIES
set_target_properties(${DOTNET_NATIVE_LIBRARY} PROPERTIES
INSTALL_RPATH "$ORIGIN")
endif()

Expand All @@ -122,7 +124,7 @@ list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}")
# Swig wrap all libraries
foreach(SUBPROJECT IN ITEMS Foo Bar FooBar)
add_subdirectory(${SUBPROJECT}/dotnet)
target_link_libraries(mizux-dotnetnative-native PRIVATE dotnet_${SUBPROJECT})
target_link_libraries(${DOTNET_NATIVE_LIBRARY} PRIVATE dotnet_${SUBPROJECT})
endforeach()

file(COPY ${PROJECT_SOURCE_DIR}/dotnet/logo.png DESTINATION ${PROJECT_BINARY_DIR}/dotnet)
Expand All @@ -137,7 +139,7 @@ file(MAKE_DIRECTORY ${DOTNET_PACKAGES_DIR})
# CMake variable(s) (@PROJECT_NAME@) that configure_file() can manage and
# generator expression ($<TARGET_FILE:...>) that file(GENERATE) can manage.
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PACKAGE}.runtime.csproj.in
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PROJECT}.runtime.csproj.in
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj.in
@ONLY)
file(GENERATE
Expand All @@ -161,7 +163,7 @@ add_custom_command(
DEPENDS
${PROJECT_BINARY_DIR}/dotnet/Directory.Build.props
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj
mizux-dotnetnative-native
${DOTNET_NATIVE_LIBRARY}
BYPRODUCTS
${DOTNET_NATIVE_PROJECT_DIR}/bin
${DOTNET_NATIVE_PROJECT_DIR}/obj
Expand Down
6 changes: 3 additions & 3 deletions docs/full_pipeline.dot
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ digraph CMake {

LINUX_WRAPPER_CPP [label="fooCSHARP_wrap.cxx"];
LINUX_WRAPPER_OBJ [label="fooCSHARP_wrap.cxx.o"];
LINUX_WRAPPER_LIB [label="mizux-dotnetnative-native.so\nruntimes/linux-x64/native/", shape=component];
LINUX_WRAPPER_LIB [label="DOTNET_NATIVE_LIBRARY.so\nruntimes/linux-x64/native/", shape=component];

LINUX_WRAPPER_CPP -> LINUX_WRAPPER_OBJ [label="g++", style=bold];
LINUX_WRAPPER_OBJ -> LINUX_WRAPPER_LIB [label="ld", style=bold];
Expand Down Expand Up @@ -55,7 +55,7 @@ digraph CMake {

OSX_WRAPPER_CPP [label="fooCSHARP_wrap.cxx"];
OSX_WRAPPER_OBJ [label="fooCSHARP_wrap.cxx.o"];
OSX_WRAPPER_LIB [label="mizux-dotnetnative-native.dylib\nruntimes/osx-x64/native/", shape=component];
OSX_WRAPPER_LIB [label="DOTNET_NATIVE_LIBRARY.dylib\nruntimes/osx-x64/native/", shape=component];

OSX_WRAPPER_CPP -> OSX_WRAPPER_OBJ [label="g++", style=bold];
OSX_WRAPPER_OBJ -> OSX_WRAPPER_LIB [label="ld", style=bold];
Expand Down Expand Up @@ -91,7 +91,7 @@ digraph CMake {

WIN_WRAPPER_CPP [label="fooCSHARP_wrap.cxx"];
WIN_WRAPPER_OBJ [label="fooCSHARP_wrap.cxx.o"];
WIN_WRAPPER_LIB [label="mizux-dotnetnative-native.dll\nruntimes/win-x64/native/", shape=component];
WIN_WRAPPER_LIB [label="DOTNET_NATIVE_LIBRARY.dll\nruntimes/win-x64/native/", shape=component];

WIN_WRAPPER_CPP -> WIN_WRAPPER_OBJ [label="g++", style=bold];
WIN_WRAPPER_OBJ -> WIN_WRAPPER_LIB [label="ld", style=bold];
Expand Down
2 changes: 1 addition & 1 deletion docs/local_pipeline.dot
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ digraph CMake {

WRAPPER_CPP [label="fooCSHARP_wrap.cxx"];
WRAPPER_OBJ [label="fooCSHARP_wrap.cxx.o"];
WRAPPER_LIB [label="mizux-dotnetnative-native.so\nruntimes/linux-x64/native/", shape=component];
WRAPPER_LIB [label="DOTNET_NATIVE_LIBRARY.so\nruntimes/linux-x64/native/", shape=component];

CPP -> WRAPPER_CPP [label="include", style=dashed];
CPP -> LIB [label="g++, ld", style=bold];
Expand Down
4 changes: 2 additions & 2 deletions dotnet/Mizux.DotnetNative.runtime.csproj.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<OutputType>Library</OutputType>
@DOTNET_TFM@
<RuntimeIdentifier>@DOTNET_RID@</RuntimeIdentifier>
<AssemblyName>mizux-dotnetnative-native</AssemblyName>
<AssemblyName>@DOTNET_NATIVE_LIBRARY@</AssemblyName>
<Version>@PROJECT_VERSION@</Version>

<!-- Nuget Properties -->
Expand All @@ -24,7 +24,7 @@
<!-- Native library must be in native directory... -->
<!-- If project is built as a STATIC_LIBRARY (e.g. Windows) then we don't have to include it -->
<Content Include="
$<TARGET_FILE:mizux-dotnetnative-native>
$<TARGET_FILE:@DOTNET_NATIVE_LIBRARY@>
$<$<STREQUAL:$<TARGET_PROPERTY:Bar,TYPE>,SHARED_LIBRARY>:;$<TARGET_SONAME_FILE:Bar>>
$<$<STREQUAL:$<TARGET_PROPERTY:Foo,TYPE>,SHARED_LIBRARY>:;$<TARGET_SONAME_FILE:Foo>>
$<$<STREQUAL:$<TARGET_PROPERTY:FooBar,TYPE>,SHARED_LIBRARY>:;$<TARGET_SONAME_FILE:FooBar>>
Expand Down

2 comments on commit bd8b83f

@Mizux
Copy link
Owner Author

@Mizux Mizux commented on bd8b83f Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #8

@JensMunkHansen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much

Please sign in to comment.