Skip to content

Commit 26b750d

Browse files
Merge pull request #16 from Neko-Box-Coder/CrossPlatformDefine
Adding cross platform defines
2 parents 76fa887 + df4aabc commit 26b750d

File tree

14 files changed

+256
-43
lines changed

14 files changed

+256
-43
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ add_executable(runcpp2
8484
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/ProfilesCompilesFiles.cpp"
8585
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/FilesTypesInfo.cpp"
8686
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/StageInfo.cpp"
87+
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/Data/ProfilesDefines.cpp"
8788
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/ProfileHelper.cpp"
8889
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/CompilingLinking.cpp"
8990
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/ConfigParsing.cpp"

DefaultYAMLs/DefaultScriptInfo.yaml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
# NOTE: All command substitutions are passed directly to the shell.
2+
# Be cautious when using user-provided input in your build commands to avoid potential security risks.
3+
14
# NOTE: All the options here are defaulted to empty.
25
# You can uncomment different sections to set what you want.
36

47
# # (Optional) Language of the script
58
# Language: "c++"
69

710
# # (Optional) Preferred profiles for the script for each platform.
8-
# # A special profile name "All" can be used to allow any profile.
9-
# # Default profiles are listed in user config file,
11+
# # Profiles are listed in user config file,
1012
# # which can be retreived by running `runcpp2 --show-config-path`
1113
# # Each of the platform dependent settings can be listed under
12-
# # - All
14+
# # - Default
1315
# # - Windows
1416
# # - Linux
1517
# # - MacOS
@@ -22,8 +24,8 @@
2224
# # (Optional) Override the default compile flags for each platform.
2325
# OverrideCompileFlags:
2426
# # Target Platform
25-
# All:
26-
# # Profile with the respective flags to override. ("All" for any profile)
27+
# Default:
28+
# # Profile with the respective flags to override. ("Default" for any profile)
2729
# "g++":
2830
# # (Optional) Flags to be removed from the default compile flags, separated by space
2931
# Remove: ""
@@ -34,8 +36,8 @@
3436
# # (Optional) Override the default link flags for each platform.
3537
# OverrideLinkFlags:
3638
# # Target Platform
37-
# All:
38-
# # Profile with the respective flags to override ("All" for any profile)
39+
# Default:
40+
# # Profile with the respective flags to override ("Default" for any profile)
3941
# "g++":
4042
# # (Optional) Flags to be removed from the default link flags, separated by space
4143
# Remove: ""
@@ -47,11 +49,21 @@
4749
# # (Optional) Other source files (relative to script file path) to be compiled.
4850
# OtherFilesToBeCompiled:
4951
# # Target Platform
50-
# All:
52+
# Default:
5153
# # Target Profile
52-
# All:
54+
# Default:
5355
# - "./AnotherSourceFile.cpp"
5456

57+
# # (Optional) Define cross-compiler defines for each platform and profile.
58+
# # Defines can be specified as just a name or as a name-value pair.
59+
# Defines:
60+
# # Target Platform (Default, Windows, Linux, MacOS, or Unix)
61+
# Default:
62+
# # Profile name (e.g., "g++", "clang++", "msvc", or "Default" for any profile)
63+
# "Default":
64+
# - "EXAMPLE_DEFINE" # Define without a value
65+
# - "VERSION_MAJOR=1" # Define with a value
66+
5567
# # (Optional) The list of dependencies needed by the script
5668
# Dependencies:
5769
# # Dependency name
@@ -79,7 +91,7 @@
7991
# # (Optional if LibraryType is Header) Link properties of the dependency
8092
# LinkProperties:
8193
# # Properties for searching the library binary for the profile
82-
# # You can also use "All" if all compilers use the same values
94+
# # You can also use "Default" if all compilers use the same values
8395
# "g++":
8496
# # The library names to be searched for when linking against the script.
8597
# # Binaries with linkable extension that contains one of the names will be linked
@@ -94,24 +106,24 @@
94106
#
95107
# # (Optional) Additional link flags for this dependency for each platform
96108
# AdditionalLinkOptions:
97-
# All: []
109+
# Default: []
98110
#
99111
# # (Optional) Setup commands are run once when the dependency is populated
100112
# Setup:
101113
# # Target Platform
102-
# All:
114+
# Default:
103115
# # Setup shell commands for the specified profile.
104-
# # All commands are run in the dependency folder
105-
# # You can also use "All" if all the compilers run the same setup commands
116+
# # Default commands are run in the dependency folder
117+
# # You can also use "Default" if all the compilers run the same setup commands
106118
# "g++":
107119
# - "mkdir build"
108120
#
109121
#
110122
# # (Optional) Build commands are run every time before the script is being built
111123
# Build:
112124
# # Target Platform
113-
# All:
114-
# # Target Profile ("All" for any profile)
125+
# Default:
126+
# # Target Profile ("Default" for any profile)
115127
# "g++":
116128
# - "cd build && cmake .."
117129
# - "cd build && cmake --build ."

DefaultYAMLs/DefaultUserConfig.yaml

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Templates:
66
"g++_CompileRunParts": &g++_CompileRunParts
77
- Type: Once
88
CommandPart: "{Executable} -c {CompileFlags}"
9+
- Type: Repeats
10+
CommandPart: " -D{DefineNameOnly}="
11+
- Type: Repeats
12+
CommandPart: " \"-D{DefineName}={DefineValue}\""
913
- Type: Repeats
1014
CommandPart: " -I\"{IncludeDirectoryPath}\""
1115
- Type: Once
@@ -20,6 +24,10 @@ Templates:
2024
"vs2022_v17+_CompileRunParts": &vs2022_v17+_CompileRunParts
2125
- Type: Once
2226
CommandPart: "{Executable} /c {CompileFlags}"
27+
- Type: Repeats
28+
CommandPart: " /D{DefineNameOnly}="
29+
- Type: Repeats
30+
CommandPart: " \"/D{DefineName}={DefineValue}\""
2331
- Type: Repeats
2432
CommandPart: " /I\"{IncludeDirectoryPath}\""
2533
- Type: Once
@@ -37,15 +45,15 @@ Templates:
3745

3846
# TODO: Add support for custom platform
3947
# Each of the platform dependent settings can be listed under
40-
# - All
48+
# - Default
4149
# - Windows
4250
# - Linux
4351
# - MacOS
4452
# - Unix
4553
FilesTypes: &CommonFilesTypes
4654
ObjectLinkFile:
4755
Prefix:
48-
All: ""
56+
Default: ""
4957
Extension:
5058
Windows: ".obj"
5159
Unix: ".o"
@@ -91,6 +99,10 @@ Templates:
9199
Windows: ""
92100
Unix: ""
93101

102+
# WARNING: All command substitutions in this file are passed directly to the shell.
103+
# Exercise caution when using variables or user-provided input in your build commands
104+
# to prevent potential security vulnerabilities.
105+
94106
# A profile to be used if not specified while running the build script
95107
PreferredProfile: "g++"
96108

@@ -111,12 +123,12 @@ Profiles:
111123
# (Optional) The commands to run in **shell** before calling the compiler/linker for each platform.
112124
# This is run inside the .runcpp2 directory where the build happens.
113125
# Setup:
114-
# All: []
126+
# Default: []
115127

116128
# (Optional) The commands to run in **shell** after calling the compiler/linker for each platform.
117129
# This is run inside the .runcpp2 directory where the build happens.
118130
# Cleanup:
119-
# All: []
131+
# Default: []
120132

121133
# The file properties for the object files for each platform.
122134
FilesTypes: *CommonFilesTypes
@@ -126,16 +138,19 @@ Profiles:
126138
# (Optional) The command to run together before running each compile command
127139
# in **shell** for each platform
128140
# PreRun:
129-
# All: ""
141+
# Default: ""
130142

131143
# Shell command to use for checking if the executable exists or not
132144
CheckExistence:
133-
All: "g++ -v"
145+
Default: "g++ -v"
134146

135147
# Here are a list of substitution strings for RunParts, Setup and Cleanup
136148
# {Executable}: Compiler executable
137149
# {CompileFlags}: Compile flags from config and override
138150
# {IncludeDirectoryPath}: Path to include directories for both the script and dependencies
151+
# {DefineNameOnly}: Name of a macro to be defined without a value (equivalent to #define X)
152+
# {DefineName}: Name of a macro to be defined with a value
153+
# {DefineValue}: Value of a macro to be defined (used with {DefineName})
139154

140155
# {InputFileName}: Name of the input file (without directory path and extension)
141156
# {InputFileExtension}: Extension of the input file
@@ -148,7 +163,7 @@ Profiles:
148163
# {OutputFilePath}: (Similar to previous)
149164
CompileTypes:
150165
Executable:
151-
All:
166+
Default:
152167
Flags: "-std=c++17 -Wall -Werror"
153168
Executable: "g++"
154169
RunParts: *g++_CompileRunParts
@@ -159,14 +174,14 @@ Profiles:
159174
# This is run inside the .runcpp2 directory where the build happens.
160175
# Cleanup: []
161176
Static:
162-
All:
177+
Default:
163178
Flags: "-std=c++17 -Wall -Werror"
164179
Executable: "g++"
165180
RunParts: *g++_CompileRunParts
166181
# Setup: []
167182
# Cleanup: []
168183
Shared:
169-
All:
184+
Default:
170185
Flags: "-std=c++17 -Wall -Werror -fpic"
171186
Executable: "g++"
172187
RunParts: *g++_CompileRunParts
@@ -178,11 +193,11 @@ Profiles:
178193
# (Optional) The command to run together before running each link command
179194
# in **shell** for each platform
180195
# PreRun:
181-
# All: ""
196+
# Default: ""
182197

183198
# Shell command to use for checking if the executable exists or not
184199
CheckExistence:
185-
All: "g++ -v"
200+
Default: "g++ -v"
186201

187202
# Here are a list of substitution strings for RunParts, Setup and Cleanup
188203
# {Executable}: Linker executable
@@ -227,7 +242,7 @@ Profiles:
227242
# Setup: []
228243
# Cleanup: []
229244
Static:
230-
All:
245+
Default:
231246
Flags: ""
232247
Executable: "g++"
233248
RunParts: *g++_LinkRunParts

Examples/test.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
77
OtherFilesToBeCompiled:
88
# Target Platform
9-
All:
9+
Default:
1010
# Target Profile
1111
"g++":
1212
- "./OtherSources/AnotherSourceFileGcc.cpp"
13-
All:
13+
Default:
1414
- "./AnotherSourceFile.cpp"
1515
- "./AnotherSourceFile2.cpp"
1616
17+
Defines:
18+
Default:
19+
# Turns into `TEST_DEF=\"Test Define Working\"` in shell
20+
"Default": ["TEST_DEF=\\\"Test Define Working\\\""]
21+
1722
Dependencies:
1823
- Name: ssLogger
1924
Platforms: [Windows, Linux, MacOS]
@@ -23,17 +28,17 @@
2328
LibraryType: Shared
2429
IncludePaths: ["Include"]
2530
LinkProperties:
26-
"All":
31+
"Default":
2732
SearchLibraryNames: ["ssLogger"]
2833
ExcludeLibraryNames: ["ssLogger_SRC"]
2934
SearchDirectories: ["./build", "./build/Debug", "./build/Release"]
3035
Setup:
31-
All:
32-
"All":
36+
Default:
37+
"Default":
3338
- "mkdir build"
3439
Build:
35-
All:
36-
"All":
40+
Default:
41+
"Default":
3742
- "cd build && cmake .. -DssLOG_BUILD_TYPE=SHARED"
3843
- "cd build && cmake --build . --config Release -j 16"
3944
*/
@@ -52,6 +57,7 @@
5257
int main(int argc, char* argv[])
5358
{
5459
std::cout << "Hello World" << std::endl;
60+
std::cout << TEST_DEF << std::endl;
5561

5662
for(int i = 0; i < argc; ++i)
5763
std::cout << "Arg" << i << ": " << argv[i] << std::endl;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef RUNCPP2_DATA_PROFILES_DEFINES_HPP
2+
#define RUNCPP2_DATA_PROFILES_DEFINES_HPP
3+
4+
#include "runcpp2/Data/ParseCommon.hpp"
5+
#include "ryml.hpp"
6+
#include <unordered_map>
7+
#include <vector>
8+
#include <string>
9+
10+
namespace runcpp2
11+
{
12+
namespace Data
13+
{
14+
struct Define
15+
{
16+
std::string Name;
17+
std::string Value;
18+
bool HasValue;
19+
};
20+
21+
class ProfilesDefines
22+
{
23+
public:
24+
std::unordered_map<ProfileName, std::vector<Define>> Defines;
25+
26+
bool ParseYAML_Node(ryml::ConstNodeRef& node);
27+
std::string ToString(std::string indentation) const;
28+
};
29+
}
30+
}
31+
32+
#endif

Include/runcpp2/Data/ScriptInfo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "runcpp2/Data/ProfilesFlagsOverride.hpp"
66
#include "runcpp2/Data/ParseCommon.hpp"
77
#include "runcpp2/Data/ProfilesCompilesFiles.hpp"
8+
#include "runcpp2/Data/ProfilesDefines.hpp"
89

910
#include <string>
1011
#include <vector>
@@ -27,6 +28,7 @@ namespace runcpp2
2728

2829
std::vector<DependencyInfo> Dependencies;
2930

31+
std::unordered_map<PlatformName, ProfilesDefines> Defines;
3032

3133
bool Populated = false;
3234

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Everything is documented as comment in the template but here's a quick summary.
7171
- `OtherFilesToBeCompiled`: Other source files you wish to be compiled.
7272
- `Dependencies`: Any external libraries you wish to use. See next section.
7373

74+
> [!NOTE]
75+
> Settings in the script info are passed directly to the shell. Be cautious when using user-provided input in your build commands.
76+
7477
#### 4. Using External Libraries
7578

7679
To use any external libraries, you need to specify them in the Dependencies section.

0 commit comments

Comments
 (0)