diff --git a/buildcc/lib/target/include/target/target.h b/buildcc/lib/target/include/target/target.h index 5d2dce32..2affbe93 100644 --- a/buildcc/lib/target/include/target/target.h +++ b/buildcc/lib/target/include/target/target.h @@ -91,7 +91,7 @@ class Target { const fs::path &absolute_output_path); // * Headers - void AddHeader(const std::string &relative_filename, + void AddHeader(const fs::path &relative_filename, const fs::path &relative_to_target_path = ""); void AddHeaderAbsolute(const fs::path &absolute_filepath); diff --git a/buildcc/lib/target/src/target/include_dir.cpp b/buildcc/lib/target/src/target/include_dir.cpp index aefebe09..33991b22 100644 --- a/buildcc/lib/target/src/target/include_dir.cpp +++ b/buildcc/lib/target/src/target/include_dir.cpp @@ -32,7 +32,7 @@ void Target::AddHeaderAbsolute(const fs::path &absolute_filepath) { internal::add_path(absolute_filepath, current_header_files_); } -void Target::AddHeader(const std::string &relative_filename, +void Target::AddHeader(const fs::path &relative_filename, const fs::path &relative_to_target_path) { env::log_trace(name_, __FUNCTION__); diff --git a/buildcc/targets/CMakeLists.txt b/buildcc/targets/CMakeLists.txt index d6e045ce..e383e8eb 100644 --- a/buildcc/targets/CMakeLists.txt +++ b/buildcc/targets/CMakeLists.txt @@ -1,10 +1,8 @@ set(TARGET_SPECIALIZED_SRCS - include/targets/target_constants.h include/targets/target_gcc.h include/targets/target_msvc.h include/targets/target_generic.h include/targets/target_custom.h - include/targets/target_utils.h ) if(${BUILDCC_BUILD_AS_SINGLE_LIB}) diff --git a/buildcc/targets/include/targets/target_constants.h b/buildcc/targets/include/targets/target_constants.h deleted file mode 100644 index 0afb9bc6..00000000 --- a/buildcc/targets/include/targets/target_constants.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2021 Niket Naidu. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TARGETS_TARGET_CONSTANTS_H_ -#define TARGETS_TARGET_CONSTANTS_H_ - -#include - -namespace buildcc { - -// Extensions -constexpr std::string_view kGccStaticLibExt = ".a"; -constexpr std::string_view kGccDynamicLibExt = ".so"; -constexpr std::string_view kWinExecutableExt = ".exe"; -constexpr std::string_view kWinStaticLibExt = ".lib"; -// Why is `kWinDynamicLibExt != .dll` but `.lib` instead? -// See `kMsvcDynamicLibLinkCommand` in `target_command.h` -// IMPLIB .lib stubs are what is linked during link time -// OUT .dll needs to be present in the executable folder during runtime -constexpr std::string_view kWinDynamicLibExt = ".lib"; - -// GCC -constexpr std::string_view kGccPrefixIncludeDir = "-I"; -constexpr std::string_view kGccPrefixLibDir = "-L"; -constexpr std::string_view kGccGenericCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} -o " - "{output} -c {input}"; -constexpr std::string_view kGccExecutableLinkCommand = - "{cpp_compiler} {link_flags} {compiled_sources} -o {output} " - "{lib_dirs} {lib_deps}"; -constexpr std::string_view kGccStaticLibLinkCommand = - "{archiver} rcs {output} {compiled_sources}"; -constexpr std::string_view kGccDynamicLibCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " - "-fpic -o {output} -c {input}"; -constexpr std::string_view kGccDynamicLibLinkCommand = - "{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output}"; - -// MSVC -constexpr std::string_view kMsvcPrefixIncludeDir = "/I"; -constexpr std::string_view kMsvcPrefixLibDir = "/LIBPATH:"; -// TODO, Split this into individual CompileCommands if any differences occur -constexpr std::string_view kMsvcCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " - "/Fo{output} /c {input}"; -constexpr std::string_view kMsvcExecutableLinkCommand = - "{linker} {link_flags} {lib_dirs} /OUT:{output} {lib_deps} " - "{compiled_sources}"; -constexpr std::string_view kMsvcStaticLibLinkCommand = - "{archiver} {link_flags} /OUT:{output} {compiled_sources}"; -constexpr std::string_view kMsvcDynamicLibLinkCommand = - "{linker} /DLL {link_flags} /OUT:{output}.dll /IMPLIB:{output} " - "{compiled_sources}"; - -} // namespace buildcc - -#endif diff --git a/buildcc/targets/include/targets/target_custom.h b/buildcc/targets/include/targets/target_custom.h index c585977b..78515735 100644 --- a/buildcc/targets/include/targets/target_custom.h +++ b/buildcc/targets/include/targets/target_custom.h @@ -17,9 +17,6 @@ #ifndef TARGETS_TARGET_CUSTOM_H_ #define TARGETS_TARGET_CUSTOM_H_ -#include "target_constants.h" -#include "target_utils.h" - namespace buildcc { class Target_custom : public base::Target { @@ -28,8 +25,7 @@ class Target_custom : public base::Target { const base::Toolchain &toolchain, const std::filesystem::path &target_path_relative_to_root, std::string_view compile_command, std::string_view link_command) - : Target(Name(name, type, toolchain), type, toolchain, - target_path_relative_to_root) { + : Target(name, type, toolchain, target_path_relative_to_root) { compile_command_ = compile_command; link_command_ = link_command; } diff --git a/buildcc/targets/include/targets/target_gcc.h b/buildcc/targets/include/targets/target_gcc.h index 40608f07..caef4b74 100644 --- a/buildcc/targets/include/targets/target_gcc.h +++ b/buildcc/targets/include/targets/target_gcc.h @@ -19,11 +19,30 @@ #include "target/target.h" -#include "target_constants.h" - // TODO, Combine all of these into Target_gcc namespace buildcc { +// Extensions +constexpr std::string_view kGccStaticLibExt = ".a"; +constexpr std::string_view kGccDynamicLibExt = ".so"; + +// GCC +constexpr std::string_view kGccPrefixIncludeDir = "-I"; +constexpr std::string_view kGccPrefixLibDir = "-L"; +constexpr std::string_view kGccGenericCompileCommand = + "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} -o " + "{output} -c {input}"; +constexpr std::string_view kGccExecutableLinkCommand = + "{cpp_compiler} {link_flags} {compiled_sources} -o {output} " + "{lib_dirs} {lib_deps}"; +constexpr std::string_view kGccStaticLibLinkCommand = + "{archiver} rcs {output} {compiled_sources}"; +constexpr std::string_view kGccDynamicLibCompileCommand = + "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " + "-fpic -o {output} -c {input}"; +constexpr std::string_view kGccDynamicLibLinkCommand = + "{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output}"; + class ExecutableTarget_gcc : public base::Target { public: ExecutableTarget_gcc( diff --git a/buildcc/targets/include/targets/target_generic.h b/buildcc/targets/include/targets/target_generic.h index 2e383575..1cf709b6 100644 --- a/buildcc/targets/include/targets/target_generic.h +++ b/buildcc/targets/include/targets/target_generic.h @@ -22,9 +22,6 @@ #include "target_gcc.h" #include "target_msvc.h" -#include "target_constants.h" -#include "target_utils.h" - namespace buildcc { struct SyncTargetOptions { @@ -189,7 +186,8 @@ class Target_generic : public base::Target { env::assert_fatal(false, "Compiler ID not supported"); break; } - SyncTargets(*this, *target, { + SyncTargets(*this, *target, + { .c_compile_flags_ = true, .cpp_compile_flags_ = true, .link_flags_ = true, diff --git a/buildcc/targets/include/targets/target_msvc.h b/buildcc/targets/include/targets/target_msvc.h index 769a3a0c..6312a6ba 100644 --- a/buildcc/targets/include/targets/target_msvc.h +++ b/buildcc/targets/include/targets/target_msvc.h @@ -19,12 +19,39 @@ #include "target/target.h" -#include "target_constants.h" - // TODO, Combine all of these into Target_msvc namespace buildcc { -inline void DefaultMsvcFlags(base::Target &target) { +// MSVC Constants +constexpr std::string_view kMsvcExecutableExt = ".exe"; +constexpr std::string_view kMsvcStaticLibExt = ".lib"; +// Why is `kWinDynamicLibExt != .dll` but `.lib` instead? +// See `kMsvcDynamicLibLinkCommand` +// IMPLIB .lib stubs are what is linked during link time +// OUT .dll needs to be present in the executable folder during runtime +constexpr std::string_view kMsvcDynamicLibExt = ".lib"; + +constexpr std::string_view kMsvcObjExt = ".obj"; +constexpr std::string_view kMsvcPrefixIncludeDir = "/I"; +constexpr std::string_view kMsvcPrefixLibDir = "/LIBPATH:"; +// TODO, Split this into individual CompileCommands if any differences occur +constexpr std::string_view kMsvcCompileCommand = + "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " + "/Fo{output} /c {input}"; +constexpr std::string_view kMsvcExecutableLinkCommand = + "{linker} {link_flags} {lib_dirs} /OUT:{output} {lib_deps} " + "{compiled_sources}"; +constexpr std::string_view kMsvcStaticLibLinkCommand = + "{archiver} {link_flags} /OUT:{output} {compiled_sources}"; +constexpr std::string_view kMsvcDynamicLibLinkCommand = + "{linker} /DLL {link_flags} /OUT:{output}.dll /IMPLIB:{output} " + "{compiled_sources}"; + +inline void DefaultMsvcOptions(base::Target &target) { + target.obj_ext_ = kMsvcObjExt; + target.prefix_include_dir_ = kMsvcPrefixIncludeDir; + target.prefix_lib_dir_ = kMsvcPrefixLibDir; + target.AddCCompileFlag("/nologo"); target.AddCppCompileFlag("/nologo"); target.AddCppCompileFlag("/EHsc"); // TODO, Might need to remove this @@ -38,11 +65,10 @@ class ExecutableTarget_msvc : public base::Target { const std::filesystem::path &target_path_relative_to_root) : Target(name, base::TargetType::Executable, toolchain, target_path_relative_to_root) { - prefix_include_dir_ = kMsvcPrefixIncludeDir; - prefix_lib_dir_ = kMsvcPrefixLibDir; + target_ext_ = kMsvcExecutableExt; compile_command_ = kMsvcCompileCommand; link_command_ = kMsvcExecutableLinkCommand; - DefaultMsvcFlags(*this); + DefaultMsvcOptions(*this); } }; @@ -52,11 +78,10 @@ class StaticTarget_msvc : public base::Target { const std::filesystem::path &target_path_relative_to_root) : Target(name, base::TargetType::StaticLibrary, toolchain, target_path_relative_to_root) { - prefix_include_dir_ = kMsvcPrefixIncludeDir; - prefix_lib_dir_ = kMsvcPrefixLibDir; + target_ext_ = kMsvcStaticLibExt; compile_command_ = kMsvcCompileCommand; link_command_ = kMsvcStaticLibLinkCommand; - DefaultMsvcFlags(*this); + DefaultMsvcOptions(*this); } }; @@ -66,11 +91,10 @@ class DynamicTarget_msvc : public base::Target { const std::filesystem::path &target_path_relative_to_root) : Target(name, base::TargetType::DynamicLibrary, toolchain, target_path_relative_to_root) { - prefix_include_dir_ = kMsvcPrefixIncludeDir; - prefix_lib_dir_ = kMsvcPrefixLibDir; + target_ext_ = kMsvcDynamicLibExt; compile_command_ = kMsvcCompileCommand; link_command_ = kMsvcDynamicLibLinkCommand; - DefaultMsvcFlags(*this); + DefaultMsvcOptions(*this); } }; diff --git a/buildcc/targets/include/targets/target_utils.h b/buildcc/targets/include/targets/target_utils.h deleted file mode 100644 index e11651d4..00000000 --- a/buildcc/targets/include/targets/target_utils.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2021 Niket Naidu. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TARGETS_TARGET_UTILS_H_ -#define TARGETS_TARGET_UTILS_H_ - -#include - -#include "target/target.h" - -#include "env/host_os.h" - -namespace buildcc { - -// DONE, GCC -// DONE, MSVC -// TODO, CLANG -inline std::string_view Extension(base::TargetType type, - base::Toolchain::Id id) { - switch (id) { - // Toolchain ID: GCC - // Target Type: Executable, StaticLib, DynamicLib - // OS: Linux == Macos, Windows - case base::Toolchain::Id::Gcc: { - switch (type) { - case base::TargetType::StaticLibrary: - return kGccStaticLibExt; - case base::TargetType::DynamicLibrary: - return kGccDynamicLibExt; - case base::TargetType::Executable: - if constexpr (env::is_win()) { - return kWinExecutableExt; - } - default: - break; - } - } break; - // Toolchain ID: MSVC - // Target Type: Executable, StaticLib, DynamicLib - // OS: Windows - case base::Toolchain::Id::Msvc: - switch (type) { - case base::TargetType::StaticLibrary: - return kWinStaticLibExt; - case base::TargetType::DynamicLibrary: - return kWinDynamicLibExt; - case base::TargetType::Executable: - return kWinExecutableExt; - default: - break; - } - break; - default: - break; - } - return ""; -} - -inline std::string Name(const std::string &name, base::TargetType type, - const base::Toolchain &toolchain) { - return fmt::format("{}{}", name, Extension(type, toolchain.GetId())); -} - -} // namespace buildcc - -#endif