Skip to content

Commit c914485

Browse files
committed
feat(cxx): exclude main.cpp from lib builds with xmake
- Ensure the program is not treated as a server to allow normal termination during CI test runs
1 parent 7a7b76e commit c914485

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

template/cxx/{{cookiecutter.project_slug}}/src/main.cpp

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <spdlog/spdlog.h>
2-
32
#include <iostream>
43

54
#include "cxxopts/cxxopts.hpp"
@@ -8,16 +7,22 @@
87
int main(int argc, char* argv[]) {
98
try {
109
// Define the options
11-
cxxopts::Options options("MyProgram", "A brief description of the program");
10+
cxxopts::Options options("MyProgram", "A simple HTTP server with command-line options");
1211

13-
options.add_options()("h,help", "Print usage information")("n,name", "Name of the user", cxxopts::value<std::string>())("v,verbose", "Enable verbose mode");
12+
// Add command-line options
13+
options.add_options()
14+
("h,help", "Print usage information")
15+
("m,mode", "Server mode", cxxopts::value<std::string>()->default_value(""))
16+
("n,name", "Name of the user", cxxopts::value<std::string>())
17+
("v,verbose", "Enable verbose mode")
18+
("p,port", "Server port", cxxopts::value<int>()->default_value("8080"));
1419

1520
// Parse the arguments
1621
auto result = options.parse(argc, argv);
1722

1823
// Handle help option
1924
if (result.count("help")) {
20-
spdlog::info(options.help());
25+
std::cout << options.help() << std::endl;
2126
return 0;
2227
}
2328

@@ -35,16 +40,36 @@ int main(int argc, char* argv[]) {
3540
spdlog::debug("Verbose mode is enabled.");
3641
}
3742

38-
// Start a simple server using cpp-httplib
39-
httplib::Server svr;
43+
// Check if server mode is specified
44+
std::string mode = result["mode"].as<std::string>();
45+
if (mode == "server") {
46+
// Get port from command-line or use default
47+
int port = result["port"].as<int>();
48+
49+
// Start a simple server using cpp-httplib
50+
httplib::Server svr;
4051

41-
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
42-
res.set_content("Hello, World!", "text/plain");
43-
spdlog::info("Handled request at /");
44-
});
52+
// Root route
53+
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
54+
res.set_content("Hello, World!", "text/plain");
55+
spdlog::info("Handled request at /");
56+
});
4557

46-
spdlog::info("Starting server on http://localhost:8080");
47-
svr.listen("localhost", 8080);
58+
// Health check route
59+
svr.Get("/health", [](const httplib::Request&, httplib::Response& res) {
60+
res.set_content("Server is running", "text/plain");
61+
spdlog::info("Health check performed");
62+
});
63+
64+
spdlog::info("Starting server on http://localhost:{}", port);
65+
if (!svr.listen("localhost", port)) {
66+
spdlog::error("Failed to start server on port {}", port);
67+
return 1;
68+
}
69+
} else if (!mode.empty()) {
70+
spdlog::error("Invalid mode. Use --mode server to start the HTTP server.");
71+
return 1;
72+
}
4873

4974
} catch (const cxxopts::exceptions::exception& e) {
5075
spdlog::error("Error parsing options: {}", e.what());

template/cxx/{{cookiecutter.project_slug}}/xmake.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ target("{{cookiecutter.package_name}}_lib")
1212
set_targetdir("build/lib") -- Specify output directory
1313
add_includedirs("include", {public = true}) -- Include project's header files
1414
add_includedirs("third_party", {public = true}) -- Include project's header files
15-
add_files("src/*.cpp") -- Add project source files
15+
add_files("src/*.cpp|main.cpp") -- Add project source files, but exclude main.cpp
1616
add_headerfiles("include/**/*.h") -- Add project's header files
1717
add_headerfiles("include/**/*.hpp") -- Add project's header files
1818
add_headerfiles("third_party/**/*.h") -- Add project's header files

0 commit comments

Comments
 (0)