Skip to content

Commit 8bb1a8d

Browse files
authored
Target generic (#68)
1 parent 9f9df34 commit 8bb1a8d

File tree

15 files changed

+426
-33
lines changed

15 files changed

+426
-33
lines changed

.github/workflows/linux_gcc_cmake_build.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ jobs:
6464
run: |
6565
sudo cmake --install .
6666
67+
- name: AfterInstall Example
68+
working-directory: ${{github.workspace}}/example/gcc/AfterInstall
69+
run: |
70+
cmake -B build -G Ninja
71+
cmake --build build --parallel 2
72+
cd build
73+
./build
74+
6775
- name: Hybrid Simple Example
6876
working-directory: ${{github.workspace}}/${{env.BUILD_FOLDER}}
6977
run: |
@@ -84,10 +92,7 @@ jobs:
8492
run: |
8593
cmake --build . --target run_hybrid_customtarget_example_linux
8694
87-
- name: AfterInstall Example
88-
working-directory: ${{github.workspace}}/example/gcc/AfterInstall
95+
- name: Hybrid Generic Target Example
96+
working-directory: ${{github.workspace}}/${{env.BUILD_FOLDER}}
8997
run: |
90-
cmake -B build -G Ninja
91-
cmake --build build --parallel 2
92-
cd build
93-
./build
98+
cmake --build . --target run_hybrid_generic_example

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,5 @@ if (${BUILDCC_EXAMPLES})
124124
add_subdirectory(example/hybrid/foolib)
125125
add_subdirectory(example/hybrid/external_lib)
126126
add_subdirectory(example/hybrid/custom_target)
127+
add_subdirectory(example/hybrid/generic)
127128
endif()

buildcc/include/buildcc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@
3636
// Specialized Targets
3737
#include "target_gcc.h"
3838
#include "target_msvc.h"
39+
#include "target_generic.h"
3940

4041
// TODO, Add more specialized targets here
4142

43+
// Plugins
44+
#include "clang_compile_commands.h"
45+
4246
// BuildCC Modules
4347
#include "args.h"
4448
#include "register.h"

buildcc/lib/target/src/util/execute.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ namespace tpl = TinyProcessLib;
2525
namespace buildcc::internal {
2626

2727
bool Command::Execute(const std::string &command) {
28-
// Run the process
28+
env::assert_fatal(!command.empty(),
29+
fmt::format("Invalid command -> \"{}\"", command));
2930
buildcc::env::log_debug("system", command);
31+
3032
tpl::Process process(command);
3133
return process.get_exit_status() == 0;
3234
}

buildcc/lib/toolchain/toolchain.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Toolchain {
4444
Toolchain(const Toolchain &toolchain) = delete;
4545

4646
// Getters
47+
Id GetId() const { return id_; }
4748
const std::string &GetName() const { return name_; }
4849
const std::string &GetAsmCompiler() const { return asm_compiler_; }
4950
const std::string &GetCCompiler() const { return c_compiler_; }

buildcc/targets/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Target_specialized lib
22
m_clangtidy("target_specialized")
33
add_library(target_specialized INTERFACE
4+
target_constants.h
45
target_gcc.h
56
target_msvc.h
7+
target_generic.h
68
)
79
target_include_directories(target_specialized INTERFACE
810
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
@@ -18,8 +20,10 @@ target_link_libraries(target_specialized INTERFACE
1820
if (${BUILDCC_INSTALL})
1921
install(TARGETS target_specialized DESTINATION lib EXPORT target_specializedConfig)
2022
install(FILES
23+
${CMAKE_CURRENT_SOURCE_DIR}/target_constants.h
2124
${CMAKE_CURRENT_SOURCE_DIR}/target_gcc.h
2225
${CMAKE_CURRENT_SOURCE_DIR}/target_msvc.h
26+
${CMAKE_CURRENT_SOURCE_DIR}/target_generic.h
2327
DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}"
2428
)
2529
install(EXPORT target_specializedConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/target_specialized")

buildcc/targets/target_constants.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2021 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TARGETS_TARGET_CONSTANTS_H_
18+
#define TARGETS_TARGET_CONSTANTS_H_
19+
20+
#include <string_view>
21+
22+
namespace buildcc {
23+
24+
// GCC
25+
constexpr std::string_view kGccPrefixIncludeDir = "-I";
26+
constexpr std::string_view kGccPrefixLibDir = "-L";
27+
constexpr std::string_view kGccGenericCompileCommand =
28+
"{compiler} {preprocessor_flags} {include_dirs} {compile_flags} -o "
29+
"{output} -c {input}";
30+
constexpr std::string_view kGccExecutableLinkCommand =
31+
"{cpp_compiler} {link_flags} {compiled_sources} -o {output} "
32+
"{lib_dirs} {lib_deps}";
33+
constexpr std::string_view kGccStaticLibLinkCommand =
34+
"{archiver} rcs {output} {compiled_sources}";
35+
constexpr std::string_view kGccDynamicLibCompileCommand =
36+
"{compiler} {preprocessor_flags} {include_dirs} {compile_flags} "
37+
"-fpic -o {output} -c {input}";
38+
constexpr std::string_view kGccDynamicLibLinkCommand =
39+
"{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output}";
40+
41+
// MSVC
42+
constexpr std::string_view kMsvcPrefixIncludeDir = "/I";
43+
constexpr std::string_view kMsvcPrefixLibDir = "/LIBPATH:";
44+
// TODO, Split this into individual CompileCommands if any differences occur
45+
constexpr std::string_view kMsvcCompileCommand =
46+
"{compiler} {preprocessor_flags} {include_dirs} {compile_flags} "
47+
"/Fo{output} /c {input}";
48+
constexpr std::string_view kMsvcExecutableLinkCommand =
49+
"{linker} {link_flags} {lib_dirs} /OUT:{output} {lib_deps} "
50+
"{compiled_sources}";
51+
constexpr std::string_view kMsvcStaticLibLinkCommand =
52+
"{archiver} {link_flags} /OUT:{output} {compiled_sources}";
53+
constexpr std::string_view kMsvcDynamicLibLinkCommand =
54+
"{linker} /DLL {link_flags} /OUT:{output}.dll /IMPLIB:{output} "
55+
"{compiled_sources}";
56+
57+
} // namespace buildcc
58+
59+
#endif

buildcc/targets/target_gcc.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "target.h"
2121

22+
#include "target_constants.h"
23+
24+
// TODO, Combine all of these into Target_gcc
2225
namespace buildcc {
2326

2427
class ExecutableTarget_gcc : public base::Target {
@@ -38,9 +41,7 @@ class StaticTarget_gcc : public base::Target {
3841
target_path_relative_to_root) {}
3942

4043
private:
41-
std::string_view Link() const override {
42-
return "{archiver} rcs {output} {compiled_sources}";
43-
}
44+
std::string_view Link() const override { return kGccStaticLibLinkCommand; }
4445
};
4546

4647
class DynamicTarget_gcc : public base::Target {
@@ -52,12 +53,9 @@ class DynamicTarget_gcc : public base::Target {
5253

5354
private:
5455
std::string_view CompileCommand() const override {
55-
return "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} "
56-
"-fpic -o {output} -c {input}";
57-
}
58-
std::string_view Link() const override {
59-
return "{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output}";
56+
return kGccDynamicLibCompileCommand;
6057
}
58+
std::string_view Link() const override { return kGccDynamicLibLinkCommand; }
6159
};
6260

6361
} // namespace buildcc

buildcc/targets/target_generic.h

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2021 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TARGETS_TARGET_GENERIC_H_
18+
#define TARGETS_TARGET_GENERIC_H_
19+
20+
#include "target.h"
21+
22+
#include "target_constants.h"
23+
24+
namespace buildcc {
25+
26+
class Target_generic : public base::Target {
27+
public:
28+
Target_generic(const std::string &name, base::TargetType type,
29+
const base::Toolchain &toolchain,
30+
const std::filesystem::path &target_path_relative_to_root)
31+
: Target(name, type, toolchain, target_path_relative_to_root) {
32+
Initialize();
33+
}
34+
35+
private:
36+
std::string_view CompileCommand() const override {
37+
switch (GetToolchain().GetId()) {
38+
case base::Toolchain::Id::Gcc:
39+
return GccCompileCommand();
40+
break;
41+
case base::Toolchain::Id::Msvc:
42+
return MsvcCompileCommand();
43+
break;
44+
default:
45+
break;
46+
}
47+
return "";
48+
}
49+
std::string_view Link() const override {
50+
switch (GetToolchain().GetId()) {
51+
case base::Toolchain::Id::Gcc:
52+
return GccLink();
53+
break;
54+
case base::Toolchain::Id::Msvc:
55+
return MsvcLink();
56+
break;
57+
default:
58+
break;
59+
}
60+
return "";
61+
}
62+
63+
/**
64+
* @brief Add Toolchain-Target specific global initialization parameters here
65+
*/
66+
void Initialize() {
67+
switch (GetToolchain().GetId()) {
68+
case base::Toolchain::Id::Gcc:
69+
GccInitialize();
70+
break;
71+
case base::Toolchain::Id::Msvc:
72+
MsvcInitialize();
73+
break;
74+
default:
75+
break;
76+
}
77+
}
78+
79+
void GccInitialize() {
80+
prefix_include_dir_ = kGccPrefixIncludeDir;
81+
prefix_lib_dir_ = kGccPrefixLibDir;
82+
}
83+
std::string_view GccCompileCommand() const {
84+
switch (GetTargetType()) {
85+
case base::TargetType::Executable:
86+
case base::TargetType::StaticLibrary:
87+
return kGccGenericCompileCommand;
88+
case base::TargetType::DynamicLibrary:
89+
return kGccDynamicLibCompileCommand;
90+
default:
91+
break;
92+
}
93+
return "";
94+
}
95+
std::string_view GccLink() const {
96+
switch (GetTargetType()) {
97+
case base::TargetType::Executable:
98+
return kGccExecutableLinkCommand;
99+
case base::TargetType::StaticLibrary:
100+
return kGccStaticLibLinkCommand;
101+
case base::TargetType::DynamicLibrary:
102+
return kGccDynamicLibLinkCommand;
103+
default:
104+
break;
105+
}
106+
return "";
107+
}
108+
109+
void MsvcInitialize() {
110+
prefix_include_dir_ = kMsvcPrefixIncludeDir;
111+
prefix_lib_dir_ = kMsvcPrefixLibDir;
112+
AddCCompileFlag("/nologo");
113+
AddCppCompileFlag("/nologo");
114+
AddCppCompileFlag("/EHsc");
115+
AddLinkFlag("/nologo");
116+
}
117+
std::string_view MsvcCompileCommand() const {
118+
switch (GetTargetType()) {
119+
case base::TargetType::Executable:
120+
case base::TargetType::StaticLibrary:
121+
case base::TargetType::DynamicLibrary:
122+
return kMsvcCompileCommand;
123+
default:
124+
break;
125+
}
126+
return "";
127+
}
128+
std::string_view MsvcLink() const {
129+
switch (GetTargetType()) {
130+
case base::TargetType::Executable:
131+
return kMsvcExecutableLinkCommand;
132+
case base::TargetType::StaticLibrary:
133+
return kMsvcStaticLibLinkCommand;
134+
case base::TargetType::DynamicLibrary:
135+
return kMsvcDynamicLibLinkCommand;
136+
default:
137+
break;
138+
}
139+
return "";
140+
}
141+
};
142+
143+
} // namespace buildcc
144+
145+
#endif

buildcc/targets/target_msvc.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919

2020
#include "target.h"
2121

22-
namespace buildcc {
22+
#include "target_constants.h"
2323

24-
// TODO, Later shift this into a constants file
25-
constexpr const char *const kMsvcPrefixIncludeDir = "/I";
26-
constexpr const char *const kMsvcPrefixLibDir = "/LIBPATH:";
27-
constexpr const char *const kMsvcCompileCommand =
28-
"{compiler} {preprocessor_flags} {include_dirs} {compile_flags} "
29-
"/Fo{output} /c {input}";
24+
// TODO, Combine all of these into Target_msvc
25+
namespace buildcc {
3026

3127
class ExecutableTarget_msvc : public base::Target {
3228
public:
@@ -43,10 +39,7 @@ class ExecutableTarget_msvc : public base::Target {
4339
std::string_view CompileCommand() const override {
4440
return kMsvcCompileCommand;
4541
}
46-
std::string_view Link() const override {
47-
return "{linker} {link_flags} {lib_dirs} /OUT:{output} {lib_deps} "
48-
"{compiled_sources}";
49-
}
42+
std::string_view Link() const override { return kMsvcExecutableLinkCommand; }
5043
};
5144

5245
class StaticTarget_msvc : public base::Target {
@@ -63,9 +56,7 @@ class StaticTarget_msvc : public base::Target {
6356
std::string_view CompileCommand() const override {
6457
return kMsvcCompileCommand;
6558
}
66-
std::string_view Link() const override {
67-
return "{archiver} {link_flags} /OUT:{output} {compiled_sources}";
68-
}
59+
std::string_view Link() const override { return kMsvcStaticLibLinkCommand; }
6960
};
7061

7162
class DynamicTarget_msvc : public base::Target {
@@ -82,10 +73,7 @@ class DynamicTarget_msvc : public base::Target {
8273
std::string_view CompileCommand() const override {
8374
return kMsvcCompileCommand;
8475
}
85-
std::string_view Link() const override {
86-
return "{linker} /DLL {link_flags} /OUT:{output}.dll /IMPLIB:{output} "
87-
"{compiled_sources}";
88-
}
76+
std::string_view Link() const override { return kMsvcDynamicLibLinkCommand; }
8977
};
9078

9179
} // namespace buildcc

example/hybrid/generic/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# File
2+
*.dot
3+
*.PNG
4+
5+
# Folder
6+
*_internal_*

0 commit comments

Comments
 (0)