summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Guillon <Bernhard.Guillon@begu.org>2026-06-30 20:56:43 +0200
committerBernhard Guillon <Bernhard.Guillon@begu.org>2026-06-30 20:56:43 +0200
commit9bb17da786318cf6c056bdc8a5bfb100ae7ae81c (patch)
treed05940c8ec6f37799148e034a9a6237dd08ec6d3
parent914c5024e9005d06bb3f784c2bc230a58483fb9a (diff)
downloadcsharp-cmake-9bb17da786318cf6c056bdc8a5bfb100ae7ae81c.tar.gz
csharp-cmake-9bb17da786318cf6c056bdc8a5bfb100ae7ae81c.zip
Add Baz library with transitive PUBLIC dependency support
-rw-r--r--CMakeLists.txt1
-rw-r--r--baz/Baz.cs9
-rw-r--r--baz/CMakeLists.txt3
-rw-r--r--cmake/dotnet.cmake67
-rw-r--r--foobar/CMakeLists.txt2
-rw-r--r--foobar/FooBar.cs1
-rw-r--r--hello/CMakeLists.txt4
7 files changed, 66 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0c67db4..b3973f3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,5 +7,6 @@ set(RUNTIME_DIR ${DOTNET_ROOT}/shared/Microsoft.NETCore.App/10.0.9)
include(cmake/dotnet.cmake)
+add_subdirectory(baz)
add_subdirectory(foobar)
add_subdirectory(hello)
diff --git a/baz/Baz.cs b/baz/Baz.cs
new file mode 100644
index 0000000..f3b017f
--- /dev/null
+++ b/baz/Baz.cs
@@ -0,0 +1,9 @@
+using System;
+
+public class Baz
+{
+ public static void Print()
+ {
+ Console.WriteLine("Baz");
+ }
+}
diff --git a/baz/CMakeLists.txt b/baz/CMakeLists.txt
new file mode 100644
index 0000000..0c8065e
--- /dev/null
+++ b/baz/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_dotnet_library(Baz
+ SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Baz.cs
+)
diff --git a/cmake/dotnet.cmake b/cmake/dotnet.cmake
index 175187b..fe0432a 100644
--- a/cmake/dotnet.cmake
+++ b/cmake/dotnet.cmake
@@ -20,6 +20,7 @@ function(add_dotnet_library NAME)
OUTPUT ${OUTPUT_FILE}
COMMAND dotnet ${CSC_DLL}
${CSC_FLAGS}
+ $<TARGET_PROPERTY:${NAME},DOTNET_ALL_REFS>
-out:${OUTPUT_FILE}
-lib:${RUNTIME_DIR}
-reference:System.Private.CoreLib.dll
@@ -28,9 +29,15 @@ function(add_dotnet_library NAME)
${arg_SOURCES}
DEPENDS ${arg_SOURCES}
COMMENT "Compiling ${NAME} library with Roslyn csc..."
+ COMMAND_EXPAND_LISTS
)
add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})
+
+ set_target_properties(${NAME} PROPERTIES
+ DOTNET_ALL_REFS ""
+ DOTNET_PUBLIC_REFS ""
+ )
endfunction()
@@ -61,7 +68,7 @@ function(add_dotnet_executable NAME)
OUTPUT ${OUTPUT_FILE}
COMMAND dotnet ${CSC_DLL}
${CSC_FLAGS}
- $<TARGET_PROPERTY:${NAME},DOTNET_REFERENCES>
+ $<TARGET_PROPERTY:${NAME},DOTNET_ALL_REFS>
-out:${OUTPUT_FILE}
-lib:${RUNTIME_DIR}
-reference:System.Private.CoreLib.dll
@@ -71,35 +78,59 @@ function(add_dotnet_executable NAME)
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/HelloCMake.runtimeconfig.json
${CMAKE_BINARY_DIR}/${NAME}.runtimeconfig.json
- DEPENDS ${arg_SOURCES} $<TARGET_PROPERTY:${NAME},DOTNET_DEPENDS>
+ DEPENDS ${arg_SOURCES}
COMMENT "Compiling ${NAME} with Roslyn csc..."
+ COMMAND_EXPAND_LISTS
)
add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})
+
+ set_target_properties(${NAME} PROPERTIES
+ DOTNET_ALL_REFS ""
+ DOTNET_PUBLIC_REFS ""
+ )
endfunction()
function(target_link_dotnet_libraries TARGET)
- set(multiValueArgs LIBRARIES)
+ set(multiValueArgs PUBLIC PRIVATE INTERFACE)
cmake_parse_arguments(PARSE_ARGV 1 arg
"" "" "${multiValueArgs}"
)
- if(NOT arg_LIBRARIES)
- return()
- endif()
-
- set(REFS "")
- set(DEPS "")
- foreach(lib IN LISTS arg_LIBRARIES)
- list(APPEND REFS "-reference:${CMAKE_BINARY_DIR}/${lib}.dll")
- list(APPEND DEPS "${CMAKE_BINARY_DIR}/${lib}.dll")
- add_dependencies(${TARGET} ${lib})
+ foreach(scope PUBLIC PRIVATE INTERFACE)
+ set(libs "${arg_${scope}}")
+ if(NOT libs)
+ continue()
+ endif()
+
+ foreach(lib IN LISTS libs)
+ add_dependencies(${TARGET} ${lib})
+
+ get_target_property(LIB_PUB_REFS ${lib} DOTNET_PUBLIC_REFS)
+ if(NOT LIB_PUB_REFS)
+ set(LIB_PUB_REFS "")
+ endif()
+
+ get_target_property(CURRENT_ALL ${TARGET} DOTNET_ALL_REFS)
+ if(NOT CURRENT_ALL)
+ set(CURRENT_ALL "")
+ endif()
+ list(APPEND CURRENT_ALL "-reference:${CMAKE_BINARY_DIR}/${lib}.dll")
+ list(APPEND CURRENT_ALL ${LIB_PUB_REFS})
+ set_target_properties(${TARGET} PROPERTIES DOTNET_ALL_REFS "${CURRENT_ALL}")
+
+ if("${scope}" STREQUAL "PUBLIC" OR "${scope}" STREQUAL "INTERFACE")
+ get_target_property(CURRENT_PUB ${TARGET} DOTNET_PUBLIC_REFS)
+ if(NOT CURRENT_PUB)
+ set(CURRENT_PUB "")
+ endif()
+ list(APPEND CURRENT_PUB "-reference:${CMAKE_BINARY_DIR}/${lib}.dll")
+ list(APPEND CURRENT_PUB ${LIB_PUB_REFS})
+ list(REMOVE_DUPLICATES CURRENT_PUB)
+ set_target_properties(${TARGET} PROPERTIES DOTNET_PUBLIC_REFS "${CURRENT_PUB}")
+ endif()
+ endforeach()
endforeach()
-
- set_target_properties(${TARGET} PROPERTIES
- DOTNET_REFERENCES "${REFS}"
- DOTNET_DEPENDS "${DEPS}"
- )
endfunction()
diff --git a/foobar/CMakeLists.txt b/foobar/CMakeLists.txt
index ef616c7..7fc83cb 100644
--- a/foobar/CMakeLists.txt
+++ b/foobar/CMakeLists.txt
@@ -1,3 +1,5 @@
add_dotnet_library(FooBar
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/FooBar.cs
)
+
+target_link_dotnet_libraries(FooBar PUBLIC Baz)
diff --git a/foobar/FooBar.cs b/foobar/FooBar.cs
index fea9685..7edd61b 100644
--- a/foobar/FooBar.cs
+++ b/foobar/FooBar.cs
@@ -5,5 +5,6 @@ public class FooBar
public static void Print()
{
Console.WriteLine("FooBar");
+ Baz.Print();
}
}
diff --git a/hello/CMakeLists.txt b/hello/CMakeLists.txt
index d726950..b8db38c 100644
--- a/hello/CMakeLists.txt
+++ b/hello/CMakeLists.txt
@@ -3,6 +3,4 @@ add_dotnet_executable(HelloCMake
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Program.cs
)
-target_link_dotnet_libraries(HelloCMake
- LIBRARIES FooBar
-)
+target_link_dotnet_libraries(HelloCMake PRIVATE FooBar)