Skip to content

Commit fc457a6

Browse files
committed
Install executables in examples tree and print more meaningful error message
1 parent bc14bf9 commit fc457a6

File tree

7 files changed

+130
-47
lines changed

7 files changed

+130
-47
lines changed

examples/CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55

66
set(GRIDKIT_EXAMPLES_INSTALL_ROOT "share/gridkit/examples")
77

8+
#
9+
# gridkit_example_current_install_path(<output-variable>)
10+
#
11+
# Get the full installation destination for the current example directory.
12+
#
13+
# Result is stored in <output-variable>
14+
#
15+
macro(gridkit_example_current_install_path _out)
16+
cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR
17+
BASE_DIRECTORY ${CMAKE_SOURCE_DIR}/examples
18+
OUTPUT_VARIABLE _rel_path)
19+
set(${_out} ${GRIDKIT_EXAMPLES_INSTALL_ROOT}/${_rel_path})
20+
endmacro()
21+
822
#
923
# gridkit_example_add_file(<filename>)
1024
#
@@ -23,10 +37,7 @@ macro(gridkit_example_add_file _filename)
2337
endif()
2438
set(_binary_filepath ${CMAKE_CURRENT_BINARY_DIR}/${__fname})
2539
configure_file(${_source_filepath} ${_binary_filepath} COPYONLY)
26-
cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR
27-
BASE_DIRECTORY ${CMAKE_SOURCE_DIR}/examples
28-
OUTPUT_VARIABLE _rel_path)
29-
set(_ex_install_path ${GRIDKIT_EXAMPLES_INSTALL_ROOT}/${_rel_path})
40+
gridkit_example_current_install_path(_ex_install_path)
3041
install(FILES ${_filename} DESTINATION ${_ex_install_path})
3142
endmacro()
3243

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
gridkit_example_current_install_path(_install_path)
2+
13
add_executable(TwoBusBasic TwoBusBasic.cpp)
24
target_link_libraries(TwoBusBasic
35
GRIDKIT::phasor_dynamics_components
46
GRIDKIT::solvers_dyn)
5-
install(TARGETS TwoBusBasic RUNTIME DESTINATION bin)
7+
install(TARGETS TwoBusBasic RUNTIME DESTINATION ${_install_path})
68

79
add_executable(TwoBusBasicJson TwoBusBasicJson.cpp)
8-
target_link_libraries(TwoBusBasicJson
10+
target_link_libraries(TwoBusBasicJson
911
GRIDKIT::phasor_dynamics_components
1012
GRIDKIT::solvers_dyn)
1113
target_include_directories(TwoBusBasicJson
1214
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include)
1315
target_include_directories(TwoBusBasicJson
1416
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include)
15-
install(TARGETS TwoBusBasicJson RUNTIME DESTINATION bin)
17+
install(TARGETS TwoBusBasicJson RUNTIME DESTINATION ${_install_path})
1618
gridkit_example_add_file(TwoBusBasic.json)
1719

18-
add_test(NAME TwoBusBasicTest COMMAND TwoBusBasic)
19-
add_test(NAME TwoBusBasicJsonTest
20-
COMMAND TwoBusBasicJson TwoBusBasic.json
20+
add_test(NAME TwoBusBasic COMMAND TwoBusBasic)
21+
add_test(NAME TwoBusBasicJson
22+
COMMAND TwoBusBasicJson ${CMAKE_CURRENT_BINARY_DIR}/TwoBusBasic.json)
23+
add_test(NAME TwoBusBasicJson_no_arg
24+
COMMAND TwoBusBasicJson
2125
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

examples/PhasorDynamics/Tiny/TwoBus/Basic/TwoBusBasicJson.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,40 @@ int main(int argc, const char* argv[])
3030
using real_type = double;
3131
using index_type = size_t;
3232

33+
//
34+
// Input file
35+
//
36+
37+
std::filesystem::path input_file;
3338
if (argc < 2)
3439
{
35-
throw std::runtime_error(
36-
"\n\nUsage:\n"
37-
"\tTwoBusBasicJson <json-input-file>\n");
40+
if (std::filesystem::exists("TwoBusBasic.json"))
41+
{
42+
input_file = std::filesystem::current_path() / "TwoBusBasic.json";
43+
}
44+
else
45+
{
46+
std::cout << "\n"
47+
"ERROR: No input file found or provided.\n"
48+
"\n"
49+
"Usage:\n"
50+
" TwoBusBasicJson <json-input-file>\n"
51+
"\n"
52+
"Please provide a JSON input file as a positional command-line \n"
53+
"argument.\n"
54+
"\n"
55+
"By default this example will look for \"TwoBusBasic.json\" in the \n"
56+
"current working directory and use that if found.\n"
57+
"\n";
58+
exit(1);
59+
}
60+
}
61+
else
62+
{
63+
input_file = argv[1];
3864
}
3965

4066
std::cout << "Example: TwoBusBasicJson\n";
41-
42-
//
43-
// Input file
44-
//
45-
46-
auto input_file = std::filesystem::path(argv[1]);
4767
std::cout << "Input file: " << input_file << '\n';
4868

4969
//
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
gridkit_example_current_install_path(_install_path)
2+
13
add_executable(TwoBusIeeet1 TwoBusIeeet1.cpp)
24
target_link_libraries(TwoBusIeeet1
35
GRIDKIT::phasor_dynamics_components
46
GRIDKIT::phasor_dynamics_signal
57
GRIDKIT::solvers_dyn)
6-
install(TARGETS TwoBusIeeet1 RUNTIME DESTINATION bin)
8+
install(TARGETS TwoBusIeeet1 RUNTIME DESTINATION ${_install_path})
79

810
add_executable(TwoBusIeeet1Json TwoBusIeeet1Json.cpp)
911
target_link_libraries(TwoBusIeeet1Json
@@ -14,10 +16,12 @@ target_include_directories(TwoBusIeeet1Json
1416
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include)
1517
target_include_directories(TwoBusIeeet1Json
1618
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include)
17-
install(TARGETS TwoBusIeeet1Json RUNTIME DESTINATION bin)
19+
install(TARGETS TwoBusIeeet1Json RUNTIME DESTINATION ${_install_path})
1820
gridkit_example_add_file(TwoBusIeeet1.json)
1921

2022
add_test(NAME GenrouTest1_Ieeet1 COMMAND TwoBusIeeet1)
2123
add_test(NAME GenrouTest1_Ieeet1_Json
22-
COMMAND TwoBusIeeet1Json TwoBusIeeet1.json
24+
COMMAND TwoBusIeeet1Json ${CMAKE_CURRENT_BINARY_DIR}/TwoBusIeeet1.json)
25+
add_test(NAME GenrouTest1_Ieeet1_Json_no_arg
26+
COMMAND TwoBusIeeet1Json
2327
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/TwoBusIeeet1Json.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,40 @@ int main(int argc, const char* argv[])
2929
using real_type = double;
3030
using index_type = size_t;
3131

32-
if (argc < 2)
33-
{
34-
throw std::runtime_error(
35-
"\n\nUsage:\n"
36-
"\tTwoBusBasicJson <json-input-file>\n");
37-
}
38-
39-
std::cout << "Example: TwoBusTgov1 + IEEET1 Exciter\n";
40-
4132
//
4233
// Input file
4334
//
4435

45-
auto input_file = std::filesystem::path(argv[1]);
36+
std::filesystem::path input_file;
37+
if (argc < 2)
38+
{
39+
if (std::filesystem::exists("TwoBusIeeet1.json"))
40+
{
41+
input_file = std::filesystem::current_path() / "TwoBusIeeet1.json";
42+
}
43+
else
44+
{
45+
std::cout << "\n"
46+
"ERROR: No input file found or provided.\n"
47+
"\n"
48+
"Usage:\n"
49+
" TwoBusIeeet1Json <json-input-file>\n"
50+
"\n"
51+
"Please provide a JSON input file as a positional command-line \n"
52+
"argument.\n"
53+
"\n"
54+
"By default this example will look for \"TwoBusIeeet1.json\" in the \n"
55+
"current working directory and use that if found.\n"
56+
"\n";
57+
exit(1);
58+
}
59+
}
60+
else
61+
{
62+
input_file = argv[1];
63+
}
64+
65+
std::cout << "Example: TwoBusIeeet1Json\n";
4666
std::cout << "Input file: " << input_file << '\n';
4767

4868
//
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1+
gridkit_example_current_install_path(_install_path)
2+
13
add_executable(TwoBusTgov1 TwoBusTgov1.cpp)
2-
target_link_libraries(TwoBusTgov1
4+
target_link_libraries(TwoBusTgov1
35
GRIDKIT::phasor_dynamics_components
46
GRIDKIT::phasor_dynamics_signal
57
GRIDKIT::solvers_dyn)
6-
install(TARGETS TwoBusTgov1 RUNTIME DESTINATION bin)
8+
install(TARGETS TwoBusTgov1 RUNTIME DESTINATION ${_install_path})
79

810
add_executable(TwoBusTgov1Json TwoBusTgov1Json.cpp)
9-
target_link_libraries(TwoBusTgov1Json
11+
target_link_libraries(TwoBusTgov1Json
1012
GRIDKIT::phasor_dynamics_components
1113
GRIDKIT::phasor_dynamics_signal
1214
GRIDKIT::solvers_dyn)
1315
target_include_directories(TwoBusTgov1Json
1416
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include)
1517
target_include_directories(TwoBusTgov1Json
1618
PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include)
17-
install(TARGETS TwoBusTgov1Json RUNTIME DESTINATION bin)
19+
install(TARGETS TwoBusTgov1Json RUNTIME DESTINATION ${_install_path})
1820
gridkit_example_add_file(TwoBusTgov1.json)
1921

2022
add_test(NAME GenrouTest1_tgov1 COMMAND TwoBusTgov1)
2123
add_test(NAME GenrouTest1_tgov1_json
22-
COMMAND TwoBusTgov1Json TwoBusTgov1.json
24+
COMMAND TwoBusTgov1Json ${CMAKE_CURRENT_BINARY_DIR}/TwoBusTgov1.json)
25+
add_test(NAME GenrouTest1_tgov1_json_no_arg
26+
COMMAND TwoBusTgov1Json
2327
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

examples/PhasorDynamics/Tiny/TwoBus/Tgov1/TwoBusTgov1Json.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,40 @@ int main(int argc, const char* argv[])
3333
using real_type = double;
3434
using index_type = size_t;
3535

36-
if (argc < 2)
37-
{
38-
throw std::runtime_error(
39-
"\n\nUsage:\n"
40-
"\tTwoBusBasicJson <json-input-file>\n");
41-
}
42-
43-
std::cout << "Example: TwoBusTgov1Json \n";
44-
4536
//
4637
// Input file
4738
//
4839

49-
auto input_file = std::filesystem::path(argv[1]);
40+
std::filesystem::path input_file;
41+
if (argc < 2)
42+
{
43+
if (std::filesystem::exists("TwoBusTgov1.json"))
44+
{
45+
input_file = std::filesystem::current_path() / "TwoBusTgov1.json";
46+
}
47+
else
48+
{
49+
std::cout << "\n"
50+
"ERROR: No input file found or provided.\n"
51+
"\n"
52+
"Usage:\n"
53+
" TwoBusTgov1Json <json-input-file>\n"
54+
"\n"
55+
"Please provide a JSON input file as a positional command-line \n"
56+
"argument.\n"
57+
"\n"
58+
"By default this example will look for \"TwoBusTgov1.json\" in the \n"
59+
"current working directory and use that if found.\n"
60+
"\n";
61+
exit(1);
62+
}
63+
}
64+
else
65+
{
66+
input_file = argv[1];
67+
}
68+
69+
std::cout << "Example: TwoBusTgov1Json\n";
5070
std::cout << "Input file: " << input_file << '\n';
5171

5272
//

0 commit comments

Comments
 (0)