From 981c48dbc57c28f759d7e394b8e00578e87a6ea4 Mon Sep 17 00:00:00 2001
From: gmh5225 <2315157@qq.com>
Date: Sun, 2 Oct 2022 10:43:32 +0800
Subject: [PATCH] [feature] msvc-static
---
docs/examples/msvc-static.md | 27 +++++++++++++++++++++++++++
include/project_parser.hpp | 2 ++
src/cmake_generator.cpp | 4 ++++
src/project_parser.cpp | 2 ++
tests/CMakeLists.txt | 10 ++++++++++
tests/msvc-static/cmake.toml | 11 +++++++++++
tests/msvc-static/src/msvc-static.cpp | 5 +++++
7 files changed, 61 insertions(+)
create mode 100644 docs/examples/msvc-static.md
create mode 100644 tests/msvc-static/cmake.toml
create mode 100644 tests/msvc-static/src/msvc-static.cpp
diff --git a/docs/examples/msvc-static.md b/docs/examples/msvc-static.md
new file mode 100644
index 0000000..9436703
--- /dev/null
+++ b/docs/examples/msvc-static.md
@@ -0,0 +1,27 @@
+---
+# Automatically generated from tests/msvc-static/cmake.toml - DO NOT EDIT
+layout: default
+title: msvc static
+permalink: /examples/msvc-static
+parent: Examples
+nav_order: 8
+---
+
+# msvc static
+
+A msvc-static `cmake.toml` project:
+
+```toml
+[project]
+name = "msvc-static"
+description = "msvc static"
+
+[target.basic]
+type = "executable"
+sources = ["src/msvc-static.cpp"]
+msvc-static = true
+```
+
+
+
+This page was automatically generated from [tests/msvc-static/cmake.toml](https://github.com/build-cpp/cmkr/tree/main/tests/msvc-static/cmake.toml).
diff --git a/include/project_parser.hpp b/include/project_parser.hpp
index a4ce581..53cd214 100644
--- a/include/project_parser.hpp
+++ b/include/project_parser.hpp
@@ -103,6 +103,8 @@ struct Target {
Condition cmake_after;
ConditionVector include_before;
ConditionVector include_after;
+
+ bool allow_msvc_static = false;
};
struct Template {
diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp
index 76bd830..94a8557 100644
--- a/src/cmake_generator.cpp
+++ b/src/cmake_generator.cpp
@@ -1026,6 +1026,10 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
gen.conditional_cmake(tmplate->outline.cmake_after);
}
+ if (target.allow_msvc_static && !target.name.empty()) {
+ cmd("set_property")("TARGET", target.name, "PROPERTY", "MSVC_RUNTIME_LIBRARY", "MultiThreaded$<$:Debug>");
+ }
+
cmd("unset")("CMKR_TARGET");
cmd("unset")("CMKR_SOURCES");
}
diff --git a/src/project_parser.cpp b/src/project_parser.cpp
index a117bf6..1f50f7c 100644
--- a/src/project_parser.cpp
+++ b/src/project_parser.cpp
@@ -514,6 +514,8 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
t.optional("precompile-headers", target.precompile_headers);
t.optional("private-precompile-headers", target.private_precompile_headers);
+ t.optional("msvc-static", target.allow_msvc_static);
+
if (!target.headers.empty()) {
auto &sources = target.sources.nth(0).value();
const auto &headers = target.headers.nth(0)->second;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index bc48fde..3693efe 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -88,3 +88,13 @@ add_test(
build
)
+add_test(
+ NAME
+ msvc-static
+ WORKING_DIRECTORY
+ "${CMAKE_CURRENT_LIST_DIR}/msvc-static"
+ COMMAND
+ "$"
+ build
+)
+
diff --git a/tests/msvc-static/cmake.toml b/tests/msvc-static/cmake.toml
new file mode 100644
index 0000000..25e7af5
--- /dev/null
+++ b/tests/msvc-static/cmake.toml
@@ -0,0 +1,11 @@
+# A msvc-static `cmake.toml` project:
+
+[project]
+name = "msvc-static"
+description = "msvc static"
+
+[target.basic]
+type = "executable"
+sources = ["src/msvc-static.cpp"]
+msvc-static = true
+
diff --git a/tests/msvc-static/src/msvc-static.cpp b/tests/msvc-static/src/msvc-static.cpp
new file mode 100644
index 0000000..684ef1d
--- /dev/null
+++ b/tests/msvc-static/src/msvc-static.cpp
@@ -0,0 +1,5 @@
+#include
+
+int main() {
+ puts("Hello from cmkr(msvc-static)!");
+}