From 635f3244bac126235a52eebdb3ec1c2820fd34c9 Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Fri, 27 Mar 2026 10:03:27 +0800 Subject: [PATCH 1/8] init Signed-off-by: Shawn Yuan --- .../Microsoft.WSL.Containers.common.targets | 95 +++++++++++++++++++ .../build/cmake/wslcConfig.cmake | 67 +++++++++++++ .../native/Microsoft.WSL.Containers.targets | 45 +++++++++ .../net/Microsoft.WSL.Containers.targets | 9 ++ 4 files changed, 216 insertions(+) create mode 100644 nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index 2454f2555..3b38dfd05 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -7,4 +7,99 @@ + + + + + + + + $(OutDir) + wslc + + + + + + latest + $(WslcImageOutputDir) + + + + + + + + + + + + + + + + + + + + + + + <_WslcSourceFiles Include="$(_WslcDockerfile);$(_WslcSources)" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake new file mode 100644 index 000000000..f64db3609 --- /dev/null +++ b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake @@ -0,0 +1,67 @@ +# wslcConfig.cmake - Container image build support for CMake +# +# Provides the wslc_add_image() function for declaring container image +# build targets with incremental rebuild support. +# +# Usage: +# list(APPEND CMAKE_MODULE_PATH "/build/cmake") +# find_package(wslc REQUIRED) +# +# wslc_add_image( +# NAME my-server +# DOCKERFILE container/Dockerfile +# CONTEXT container/ +# SOURCES container/src/*.cpp container/src/*.h +# TAG latest +# OUTPUT ${CMAKE_BINARY_DIR}/images +# ) + +function(wslc_add_image) + cmake_parse_arguments( + PARSE_ARGV 0 ARG + "" # options (none) + "NAME;TAG;DOCKERFILE;CONTEXT;OUTPUT" # one-value keywords + "SOURCES" # multi-value keywords + ) + + # Validate required arguments + if(NOT ARG_NAME) + message(FATAL_ERROR "wslc_add_image: NAME is required") + endif() + if(NOT ARG_DOCKERFILE) + message(FATAL_ERROR "wslc_add_image: DOCKERFILE is required") + endif() + if(NOT ARG_CONTEXT) + message(FATAL_ERROR "wslc_add_image: CONTEXT is required") + endif() + + # Defaults + if(NOT ARG_TAG) + set(ARG_TAG "latest") + endif() + if(NOT ARG_OUTPUT) + set(ARG_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}") + endif() + + set(_image_ref "${ARG_NAME}:${ARG_TAG}") + set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") + set(_id_output "${ARG_OUTPUT}/${ARG_NAME}.id") + set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") + + # Resolve source globs to file lists + file(GLOB_RECURSE _resolved_sources ${ARG_SOURCES}) + + add_custom_command( + OUTPUT "${_marker}" + COMMAND wslc image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" + COMMAND wslc image save "${_image_ref}" -o "${_tar_output}" + COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" + DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" + COMMENT "WSLC: Building image '${_image_ref}'..." + VERBATIM + ) + + add_custom_target(wslc_image_${ARG_NAME} ALL + DEPENDS "${_marker}" + ) +endfunction() diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index 09823c7b0..abf5db776 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -23,4 +23,49 @@ + + + + + + + + + + + + + <_WslcTlogInputs Include="$(_WslcDockerfile);$(_WslcSources)" /> + + + + <_WslcDockerfileFullPath>$([System.IO.Path]::GetFullPath('$(_WslcDockerfile)')) + <_WslcMarkerFullPath>$([System.IO.Path]::GetFullPath('$(IntDir)wslc_$(_WslcName).marker')) + + + + + + + + + + + + + \ No newline at end of file diff --git a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets index 245e57a2c..27aece358 100644 --- a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets @@ -11,4 +11,13 @@ + + + + + + + + + \ No newline at end of file From 208e9ec3d3d05144373fe756342786eb69c47a5c Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Fri, 27 Mar 2026 11:23:56 +0800 Subject: [PATCH 2/8] update Signed-off-by: Shawn Yuan --- .../Microsoft.WSL.Containers.common.targets | 36 +++++++++---------- .../build/cmake/wslcConfig.cmake | 11 ++++-- .../native/Microsoft.WSL.Containers.targets | 4 +-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index 3b38dfd05..b2830b11c 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -15,6 +15,7 @@ $(OutDir) + $(ProgramW6432)\WSL\wslc.exe wslc @@ -22,7 +23,7 @@ latest - $(WslcImageOutputDir) + $(WslcImageOutputDir) @@ -31,7 +32,7 @@ BeforeTargets="WslcBuildAllImages" Condition="'@(WslcImage)' != ''"> - + Properties="Configuration=$(Configuration);Platform=$(Platform);_WslcName=%(WslcImage.Identity);_WslcTag=%(WslcImage.Tag);_WslcDockerfile=%(WslcImage.Dockerfile);_WslcContext=%(WslcImage.Context);_WslcSourceDir=%(WslcImage.Sources);_WslcOutput=%(WslcImage.OutputDir)" /> - + - <_WslcSourceFiles Include="$(_WslcDockerfile);$(_WslcSources)" /> + <_WslcSourceFiles Include="$(_WslcDockerfile);$(_WslcSourceDir)\**\*" /> @@ -73,23 +78,18 @@ - - - - - - - + + Text="WSLC: [$(_WslcName)] Image '$(_WslcName):$(_WslcTag)' built successfully." /> @@ -97,8 +97,8 @@ AfterTargets="Clean" Condition="'@(WslcImage)' != ''"> - - + + diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake index f64db3609..c139fb6dc 100644 --- a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake @@ -43,6 +43,14 @@ function(wslc_add_image) set(ARG_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}") endif() + # Find wslc CLI + if(NOT WSLC_CLI_PATH) + find_program(WSLC_CLI_PATH wslc PATHS "$ENV{ProgramW6432}/WSL" "$ENV{ProgramFiles}/WSL") + if(NOT WSLC_CLI_PATH) + message(FATAL_ERROR "wslc CLI not found. Install WSL by running: wsl --install --no-distribution") + endif() + endif() + set(_image_ref "${ARG_NAME}:${ARG_TAG}") set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") set(_id_output "${ARG_OUTPUT}/${ARG_NAME}.id") @@ -53,8 +61,7 @@ function(wslc_add_image) add_custom_command( OUTPUT "${_marker}" - COMMAND wslc image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" - COMMAND wslc image save "${_image_ref}" -o "${_tar_output}" + COMMAND "${WSLC_CLI_PATH}" image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" COMMENT "WSLC: Building image '${_image_ref}'..." diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index abf5db776..5256f3a60 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -34,12 +34,12 @@ + Properties="Configuration=$(Configuration);Platform=$(Platform);_WslcName=%(WslcImage.Identity);_WslcDockerfile=%(WslcImage.Dockerfile);_WslcSourceDir=%(WslcImage.Sources);_WslcOutput=%(WslcImage.OutputDir);_WslcTlogDir=$(TLogLocation)" /> - <_WslcTlogInputs Include="$(_WslcDockerfile);$(_WslcSources)" /> + <_WslcTlogInputs Include="$(_WslcDockerfile);$(_WslcSourceDir)\**\*" /> From af041e469fef056e494e0f01b72f4aef935aa45f Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Fri, 27 Mar 2026 12:49:49 +0800 Subject: [PATCH 3/8] update Signed-off-by: Shawn Yuan --- .../Microsoft.WSL.Containers.common.targets | 29 +++++++++++++++---- .../build/cmake/wslcConfig.cmake | 3 +- .../native/Microsoft.WSL.Containers.targets | 6 ++-- .../net/Microsoft.WSL.Containers.targets | 12 +++++++- .../Microsoft.WSL.Containers.targets | 4 +++ .../native/Microsoft.WSL.Containers.targets | 4 +++ 6 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 nuget/Microsoft.WSL.Containers/buildTransitive/Microsoft.WSL.Containers.targets create mode 100644 nuget/Microsoft.WSL.Containers/buildTransitive/native/Microsoft.WSL.Containers.targets diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index b2830b11c..4176d843b 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -27,6 +27,21 @@ + + + + + + + + + - <_WslcSourceFiles Include="$(_WslcDockerfile);$(_WslcSourceDir)\**\*" /> + <_WslcSourceDirs Include="$(_WslcSourceDir.Split(';'))" /> + <_WslcSourceFiles Include="$(_WslcDockerfile)" /> + <_WslcSourceFiles Include="%(_WslcSourceDirs.Identity)\**\*" /> @@ -73,7 +91,7 @@ Inputs="@(_WslcSourceFiles)" Outputs="$(IntDir)wslc_$(_WslcName).marker"> - + @@ -97,9 +115,10 @@ AfterTargets="Clean" Condition="'@(WslcImage)' != ''"> - - + \ No newline at end of file diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake index c139fb6dc..70240e761 100644 --- a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake @@ -52,9 +52,8 @@ function(wslc_add_image) endif() set(_image_ref "${ARG_NAME}:${ARG_TAG}") - set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") - set(_id_output "${ARG_OUTPUT}/${ARG_NAME}.id") set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") + # TODO: set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") when wslc image save is available # Resolve source globs to file lists file(GLOB_RECURSE _resolved_sources ${ARG_SOURCES}) diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index 5256f3a60..c55353dc6 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -34,12 +34,14 @@ + Properties="Configuration=$(Configuration);Platform=$(Platform);_WslcName=%(WslcImage.Identity);_WslcDockerfile=%(WslcImage.Dockerfile);_WslcSourceDir=%(WslcImage.Sources);_WslcTlogDir=$(TLogLocation)" /> - <_WslcTlogInputs Include="$(_WslcDockerfile);$(_WslcSourceDir)\**\*" /> + <_WslcTlogSourceDirs Include="$(_WslcSourceDir.Split(';'))" /> + <_WslcTlogInputs Include="$(_WslcDockerfile)" /> + <_WslcTlogInputs Include="%(_WslcTlogSourceDirs.Identity)\**\*" /> diff --git a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets index 27aece358..66501e601 100644 --- a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets @@ -15,9 +15,19 @@ + - + + + + <_WslcUpToDateDirs Include="%(WslcImage.Sources)" Separator=";" /> + + + + \ No newline at end of file diff --git a/nuget/Microsoft.WSL.Containers/buildTransitive/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/buildTransitive/Microsoft.WSL.Containers.targets new file mode 100644 index 000000000..b5ada5ff6 --- /dev/null +++ b/nuget/Microsoft.WSL.Containers/buildTransitive/Microsoft.WSL.Containers.targets @@ -0,0 +1,4 @@ + + + + diff --git a/nuget/Microsoft.WSL.Containers/buildTransitive/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/buildTransitive/native/Microsoft.WSL.Containers.targets new file mode 100644 index 000000000..0f50deabc --- /dev/null +++ b/nuget/Microsoft.WSL.Containers/buildTransitive/native/Microsoft.WSL.Containers.targets @@ -0,0 +1,4 @@ + + + + From e5f990d4f32d1a6a0a14499be29d1594ed6f1f19 Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Fri, 27 Mar 2026 15:30:00 +0800 Subject: [PATCH 4/8] resolve comments --- .../Microsoft.WSL.Containers.common.targets | 2 +- .../build/cmake/wslcConfig.cmake | 18 +++++++++++++++--- .../native/Microsoft.WSL.Containers.targets | 2 +- .../build/net/Microsoft.WSL.Containers.targets | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index 4176d843b..f7e6708ea 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -81,7 +81,7 @@ <_WslcSourceDirs Include="$(_WslcSourceDir.Split(';'))" /> <_WslcSourceFiles Include="$(_WslcDockerfile)" /> - <_WslcSourceFiles Include="%(_WslcSourceDirs.Identity)\**\*" /> + <_WslcSourceFiles Include="%(_WslcSourceDirs.Identity)\**\*" Condition="'%(_WslcSourceDirs.Identity)' != ''" /> diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake index 70240e761..512e65ab2 100644 --- a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake @@ -15,12 +15,21 @@ # TAG latest # OUTPUT ${CMAKE_BINARY_DIR}/images # ) +# +# # With explicit image registry/name (IMAGE defaults to NAME if omitted): +# wslc_add_image( +# NAME my-server +# IMAGE ghcr.io/myorg/my-server +# TAG v1.2.3 +# DOCKERFILE container/Dockerfile +# CONTEXT container/ +# ) function(wslc_add_image) cmake_parse_arguments( PARSE_ARGV 0 ARG "" # options (none) - "NAME;TAG;DOCKERFILE;CONTEXT;OUTPUT" # one-value keywords + "NAME;IMAGE;TAG;DOCKERFILE;CONTEXT;OUTPUT" # one-value keywords "SOURCES" # multi-value keywords ) @@ -36,6 +45,9 @@ function(wslc_add_image) endif() # Defaults + if(NOT ARG_IMAGE) + set(ARG_IMAGE "${ARG_NAME}") + endif() if(NOT ARG_TAG) set(ARG_TAG "latest") endif() @@ -51,12 +63,12 @@ function(wslc_add_image) endif() endif() - set(_image_ref "${ARG_NAME}:${ARG_TAG}") + set(_image_ref "${ARG_IMAGE}:${ARG_TAG}") set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") # TODO: set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") when wslc image save is available # Resolve source globs to file lists - file(GLOB_RECURSE _resolved_sources ${ARG_SOURCES}) + file(GLOB_RECURSE _resolved_sources CONFIGURE_DEPENDS ${ARG_SOURCES}) add_custom_command( OUTPUT "${_marker}" diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index c55353dc6..53bf0f6a6 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -34,7 +34,7 @@ + Properties="Configuration=$(Configuration);Platform=$(Platform);_WslcName=%(WslcImage.Identity);_WslcDockerfile=%(WslcImage.Dockerfile);_WslcSourceDir=%(WslcImage.Sources);_WslcTlogDir=$([MSBuild]::EnsureTrailingSlash('$(TLogLocation)'))" /> diff --git a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets index 66501e601..cb1500527 100644 --- a/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/net/Microsoft.WSL.Containers.targets @@ -26,7 +26,7 @@ Condition="'$(UsingMicrosoftNETSdk)' == 'true' AND '@(WslcImage)' != ''"> <_WslcUpToDateDirs Include="%(WslcImage.Sources)" Separator=";" /> - + From e00d4eda0e61208077b91f614c834161f69db0da Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Mon, 30 Mar 2026 13:04:14 +0800 Subject: [PATCH 5/8] add placeholder for wslc save step. --- .../build/Microsoft.WSL.Containers.common.targets | 9 ++++++--- .../build/cmake/wslcConfig.cmake | 6 +++++- .../build/native/Microsoft.WSL.Containers.targets | 12 ++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index f7e6708ea..9bb5a7d6d 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -100,8 +100,10 @@ ConsoleToMSBuild="true" /> @@ -117,7 +119,8 @@ diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake index 512e65ab2..eaf51cb3c 100644 --- a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake @@ -65,7 +65,8 @@ function(wslc_add_image) set(_image_ref "${ARG_IMAGE}:${ARG_TAG}") set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") - # TODO: set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") when wslc image save is available + # Uncomment when wslc image save is available: + # set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") # Resolve source globs to file lists file(GLOB_RECURSE _resolved_sources CONFIGURE_DEPENDS ${ARG_SOURCES}) @@ -73,6 +74,9 @@ function(wslc_add_image) add_custom_command( OUTPUT "${_marker}" COMMAND "${WSLC_CLI_PATH}" image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" + # Uncomment when wslc image save is available: + # COMMAND ${CMAKE_COMMAND} -E make_directory "${ARG_OUTPUT}" + # COMMAND "${WSLC_CLI_PATH}" image save -o "${_tar_output}" "${_image_ref}" COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" COMMENT "WSLC: Building image '${_image_ref}'..." diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index 53bf0f6a6..f3d2a6868 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -47,6 +47,10 @@ <_WslcDockerfileFullPath>$([System.IO.Path]::GetFullPath('$(_WslcDockerfile)')) <_WslcMarkerFullPath>$([System.IO.Path]::GetFullPath('$(IntDir)wslc_$(_WslcName).marker')) + + Date: Mon, 30 Mar 2026 16:36:09 +0800 Subject: [PATCH 6/8] merge cmake file --- .../build/cmake/wslcConfig.cmake | 89 ------------------ .../Microsoft.WSL.ContainersConfig.cmake | 91 +++++++++++++++++++ 2 files changed, 91 insertions(+), 89 deletions(-) delete mode 100644 nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake diff --git a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake b/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake deleted file mode 100644 index eaf51cb3c..000000000 --- a/nuget/Microsoft.WSL.Containers/build/cmake/wslcConfig.cmake +++ /dev/null @@ -1,89 +0,0 @@ -# wslcConfig.cmake - Container image build support for CMake -# -# Provides the wslc_add_image() function for declaring container image -# build targets with incremental rebuild support. -# -# Usage: -# list(APPEND CMAKE_MODULE_PATH "/build/cmake") -# find_package(wslc REQUIRED) -# -# wslc_add_image( -# NAME my-server -# DOCKERFILE container/Dockerfile -# CONTEXT container/ -# SOURCES container/src/*.cpp container/src/*.h -# TAG latest -# OUTPUT ${CMAKE_BINARY_DIR}/images -# ) -# -# # With explicit image registry/name (IMAGE defaults to NAME if omitted): -# wslc_add_image( -# NAME my-server -# IMAGE ghcr.io/myorg/my-server -# TAG v1.2.3 -# DOCKERFILE container/Dockerfile -# CONTEXT container/ -# ) - -function(wslc_add_image) - cmake_parse_arguments( - PARSE_ARGV 0 ARG - "" # options (none) - "NAME;IMAGE;TAG;DOCKERFILE;CONTEXT;OUTPUT" # one-value keywords - "SOURCES" # multi-value keywords - ) - - # Validate required arguments - if(NOT ARG_NAME) - message(FATAL_ERROR "wslc_add_image: NAME is required") - endif() - if(NOT ARG_DOCKERFILE) - message(FATAL_ERROR "wslc_add_image: DOCKERFILE is required") - endif() - if(NOT ARG_CONTEXT) - message(FATAL_ERROR "wslc_add_image: CONTEXT is required") - endif() - - # Defaults - if(NOT ARG_IMAGE) - set(ARG_IMAGE "${ARG_NAME}") - endif() - if(NOT ARG_TAG) - set(ARG_TAG "latest") - endif() - if(NOT ARG_OUTPUT) - set(ARG_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}") - endif() - - # Find wslc CLI - if(NOT WSLC_CLI_PATH) - find_program(WSLC_CLI_PATH wslc PATHS "$ENV{ProgramW6432}/WSL" "$ENV{ProgramFiles}/WSL") - if(NOT WSLC_CLI_PATH) - message(FATAL_ERROR "wslc CLI not found. Install WSL by running: wsl --install --no-distribution") - endif() - endif() - - set(_image_ref "${ARG_IMAGE}:${ARG_TAG}") - set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") - # Uncomment when wslc image save is available: - # set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") - - # Resolve source globs to file lists - file(GLOB_RECURSE _resolved_sources CONFIGURE_DEPENDS ${ARG_SOURCES}) - - add_custom_command( - OUTPUT "${_marker}" - COMMAND "${WSLC_CLI_PATH}" image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" - # Uncomment when wslc image save is available: - # COMMAND ${CMAKE_COMMAND} -E make_directory "${ARG_OUTPUT}" - # COMMAND "${WSLC_CLI_PATH}" image save -o "${_tar_output}" "${_image_ref}" - COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" - DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" - COMMENT "WSLC: Building image '${_image_ref}'..." - VERBATIM - ) - - add_custom_target(wslc_image_${ARG_NAME} ALL - DEPENDS "${_marker}" - ) -endfunction() diff --git a/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake b/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake index 0ea5f0642..33dd180f7 100644 --- a/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake @@ -55,3 +55,94 @@ unset(_wslcsdk_arch) unset(_wslcsdk_root) unset(_wslcsdk_include_dir) unset(_wslcsdk_lib_dir) + +# ============================================================================ +# Container Image Build Targets +# ============================================================================ +# +# Provides the wslc_add_image() function for declaring container image +# build targets with incremental rebuild support. +# +# Usage: +# find_package(Microsoft.WSL.Containers REQUIRED) +# +# wslc_add_image( +# NAME my-server +# DOCKERFILE container/Dockerfile +# CONTEXT container/ +# SOURCES container/src/*.cpp container/src/*.h +# TAG latest +# OUTPUT ${CMAKE_BINARY_DIR}/images +# ) +# +# # With explicit image registry/name (IMAGE defaults to NAME if omitted): +# wslc_add_image( +# NAME my-server +# IMAGE ghcr.io/myorg/my-server +# TAG v1.2.3 +# DOCKERFILE container/Dockerfile +# CONTEXT container/ +# ) + +function(wslc_add_image) + cmake_parse_arguments( + PARSE_ARGV 0 ARG + "" # options (none) + "NAME;IMAGE;TAG;DOCKERFILE;CONTEXT;OUTPUT" # one-value keywords + "SOURCES" # multi-value keywords + ) + + # Validate required arguments + if(NOT ARG_NAME) + message(FATAL_ERROR "wslc_add_image: NAME is required") + endif() + if(NOT ARG_DOCKERFILE) + message(FATAL_ERROR "wslc_add_image: DOCKERFILE is required") + endif() + if(NOT ARG_CONTEXT) + message(FATAL_ERROR "wslc_add_image: CONTEXT is required") + endif() + + # Defaults + if(NOT ARG_IMAGE) + set(ARG_IMAGE "${ARG_NAME}") + endif() + if(NOT ARG_TAG) + set(ARG_TAG "latest") + endif() + if(NOT ARG_OUTPUT) + set(ARG_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}") + endif() + + # Find wslc CLI + if(NOT WSLC_CLI_PATH) + find_program(WSLC_CLI_PATH wslc PATHS "$ENV{ProgramW6432}/WSL" "$ENV{ProgramFiles}/WSL") + if(NOT WSLC_CLI_PATH) + message(FATAL_ERROR "wslc CLI not found. Install WSL by running: wsl --install --no-distribution") + endif() + endif() + + set(_image_ref "${ARG_IMAGE}:${ARG_TAG}") + set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") + # Uncomment when wslc image save is available: + # set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") + + # Resolve source globs to file lists + file(GLOB_RECURSE _resolved_sources CONFIGURE_DEPENDS ${ARG_SOURCES}) + + add_custom_command( + OUTPUT "${_marker}" + COMMAND "${WSLC_CLI_PATH}" image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" + # Uncomment when wslc image save is available: + # COMMAND ${CMAKE_COMMAND} -E make_directory "${ARG_OUTPUT}" + # COMMAND "${WSLC_CLI_PATH}" image save -o "${_tar_output}" "${_image_ref}" + COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" + DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" + COMMENT "WSLC: Building image '${_image_ref}'..." + VERBATIM + ) + + add_custom_target(wslc_image_${ARG_NAME} ALL + DEPENDS "${_marker}" + ) +endfunction() From 955453955398822ac13fb3ae7656bf5738326c29 Mon Sep 17 00:00:00 2001 From: Shawn Yuan <128874481+shuaiyuanxx@users.noreply.github.com> Date: Mon, 30 Mar 2026 17:06:39 +0800 Subject: [PATCH 7/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../build/native/Microsoft.WSL.Containers.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index f3d2a6868..daf072efe 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -41,7 +41,7 @@ <_WslcTlogSourceDirs Include="$(_WslcSourceDir.Split(';'))" /> <_WslcTlogInputs Include="$(_WslcDockerfile)" /> - <_WslcTlogInputs Include="%(_WslcTlogSourceDirs.Identity)\**\*" /> + <_WslcTlogInputs Condition="'%(_WslcTlogSourceDirs.Identity)' != ''" Include="%(_WslcTlogSourceDirs.Identity)\**\*" /> From d3dc0e60c7a5d67611f97e0fd12995171c2edd42 Mon Sep 17 00:00:00 2001 From: Shawn Yuan Date: Tue, 31 Mar 2026 09:43:45 +0800 Subject: [PATCH 8/8] remove comments for wslc save --- .../build/Microsoft.WSL.Containers.common.targets | 9 ++------- .../build/native/Microsoft.WSL.Containers.targets | 11 ----------- .../cmake/Microsoft.WSL.ContainersConfig.cmake | 10 ++++------ 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets index 9bb5a7d6d..810c31260 100644 --- a/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets +++ b/nuget/Microsoft.WSL.Containers/build/Microsoft.WSL.Containers.common.targets @@ -99,17 +99,15 @@ - + Text="WSLC: [$(_WslcName)] Image '$(_WslcName):$(_WslcTag)' saved to '$(_WslcOutput)\$(_WslcName).tar'." /> @@ -118,10 +116,7 @@ Condition="'@(WslcImage)' != ''"> - \ No newline at end of file diff --git a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets index f3d2a6868..480d85e5e 100644 --- a/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets +++ b/nuget/Microsoft.WSL.Containers/build/native/Microsoft.WSL.Containers.targets @@ -47,10 +47,7 @@ <_WslcDockerfileFullPath>$([System.IO.Path]::GetFullPath('$(_WslcDockerfile)')) <_WslcMarkerFullPath>$([System.IO.Path]::GetFullPath('$(IntDir)wslc_$(_WslcName).marker')) - - - diff --git a/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake b/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake index 33dd180f7..07a38e4ae 100644 --- a/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake +++ b/nuget/Microsoft.WSL.Containers/cmake/Microsoft.WSL.ContainersConfig.cmake @@ -124,8 +124,7 @@ function(wslc_add_image) set(_image_ref "${ARG_IMAGE}:${ARG_TAG}") set(_marker "${CMAKE_CURRENT_BINARY_DIR}/wslc_${ARG_NAME}.marker") - # Uncomment when wslc image save is available: - # set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") + set(_tar_output "${ARG_OUTPUT}/${ARG_NAME}.tar") # Resolve source globs to file lists file(GLOB_RECURSE _resolved_sources CONFIGURE_DEPENDS ${ARG_SOURCES}) @@ -133,12 +132,11 @@ function(wslc_add_image) add_custom_command( OUTPUT "${_marker}" COMMAND "${WSLC_CLI_PATH}" image build -t "${_image_ref}" -f "${ARG_DOCKERFILE}" "${ARG_CONTEXT}" - # Uncomment when wslc image save is available: - # COMMAND ${CMAKE_COMMAND} -E make_directory "${ARG_OUTPUT}" - # COMMAND "${WSLC_CLI_PATH}" image save -o "${_tar_output}" "${_image_ref}" + COMMAND ${CMAKE_COMMAND} -E make_directory "${ARG_OUTPUT}" + COMMAND "${WSLC_CLI_PATH}" image save -o "${_tar_output}" "${_image_ref}" COMMAND ${CMAKE_COMMAND} -E touch "${_marker}" DEPENDS ${_resolved_sources} "${ARG_DOCKERFILE}" - COMMENT "WSLC: Building image '${_image_ref}'..." + COMMENT "WSLC: Building and saving image '${_image_ref}' to '${_tar_output}'..." VERBATIM )