Skip to content

Commit

Permalink
Clean up and improvements to cmake config
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Sep 9, 2022
1 parent fc5c4a0 commit 0f9afc9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 65 deletions.
118 changes: 67 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,35 @@ if(NOT DEFINED SLANG_MASTER_PROJECT)
endif()
endif()

# Protect against in-tree builds.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(
FATAL_ERROR "In-source builds are not supported. You may need to delete "
"'CMakeCache.txt' and 'CMakeFiles/' before building again.")
endif()

# Set path for additional cmake modules.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

# Determine our project version by looking at git tags.
include(gitversion)
get_git_version(SLANG_VERSION_MAJOR SLANG_VERSION_MINOR SLANG_VERSION_PATCH
SLANG_VERSION_HASH SLANG_VERSION_STRING)

# Set the default build type if none is set explicitly, but only for
# single-config generators.
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(SLANG_MASTER_PROJECT
AND NOT isMultiConfig
AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Release
CACHE
STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif()

project(
slang
VERSION ${SLANG_VERSION_STRING}
Expand Down Expand Up @@ -46,81 +70,78 @@ set(SLANG_SANITIZERS
""
CACHE STRING "List of Clang sanitizers to include in build")

# Default build type if none is set
if(SLANG_MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Build type (Release/Debug/RelWithDebInfo/MinSizeRel)" FORCE)
endif()

# Include built-in cmake modules.
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# Variables used by subdirectories.
set(SCRIPTS_DIR ${PROJECT_SOURCE_DIR}/scripts)

if(NOT SLANG_EXPORT_NAME)
set(SLANG_EXPORT_NAME "${PROJECT_NAME}Targets")
endif()

# Find Python
# Find Python. If we're building python bindings we need the development modules
# as well.
if(SLANG_INCLUDE_PYLIB)
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
else()
find_package(Python REQUIRED COMPONENTS Interpreter)
endif()

# Set saner / consistent build directories on all platforms
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO
${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

# Defaults for a bunch of Windows-specific junk
foreach(
var
CMAKE_RUNTIME_OUTPUT_DIRECTORY
CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE
CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO
CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL
CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG)
if(NOT ${var})
set(${var} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
endif()
endforeach()
foreach(
var
CMAKE_ARCHIVE_OUTPUT_DIRECTORY
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG
CMAKE_LIBRARY_OUTPUT_DIRECTORY
CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE
CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
CMAKE_LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL
CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG)
if(NOT ${var})
set(${var} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
endif()
endforeach()

# The Unicode defines need to be set for all modern Windows projects (newer than
# Windows 98).
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_definitions(/DNOMINMAX)
add_definitions(/DUNICODE)
add_definitions(/D_UNICODE)
add_definitions(/DWIN32_LEAN_AND_MEAN)
add_definitions(/DNTDDI_VERSION=0x06010000)
add_definitions(/D_WIN32_WINNT=0x0601)
add_definitions(/D_SCL_SECURE_NO_WARNINGS)
add_definitions(/D_CRT_SECURE_NO_WARNINGS)
add_definitions(/D_CRT_SECURE_NO_DEPRECATE)
endif()

# Always require C++17 or later, no extensions
# Always require C++17 or later, no extensions.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# Prefer the latest standard, and enable full conformance
add_compile_options(/std:c++17)
add_compile_options(/utf-8)
# Disable MSVC language extensions, which for some reason are not covered by
# the CMAKE_CXX_EXTENSIONS option.
add_compile_options(/permissive-)
add_compile_options(/Zc:__cplusplus)
add_compile_options(/Zc:externConstexpr)
add_compile_options(/Zc:inline)
add_compile_options(/Gy) # Function-level linking
add_compile_options(/D_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
add_compile_options(/Zc:preprocessor)
add_compile_options(/Zc:throwingNew)
add_compile_options(/utf-8)

# Ignore warnings in external headers
add_compile_options(/experimental:external /external:anglebrackets
/external:W0)
add_compile_options(/external:anglebrackets /external:W0)

if(CMAKE_BUILD_TYPE MATCHES "Debug")
# Use fast linking
Expand Down Expand Up @@ -242,11 +263,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-Wno-maybe-uninitialized)
endif()

if(CMAKE_GENERATOR MATCHES "Visual Studio")
# Only MSBuild needs this, other generators will compile one file at a time
add_compile_options("/MP")
endif()

if(NOT SLANG_USE_SYSTEM_LIBS)
if(SLANG_INCLUDE_INSTALL)
set(FMT_INSTALL ON)
Expand Down
2 changes: 2 additions & 0 deletions include/slang/util/OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class OS {
}
}

static std::string getEnv(const std::string& name);

static auto captureOutput() {
capturedStdout.clear();
capturedStderr.clear();
Expand Down
2 changes: 1 addition & 1 deletion source/binding/AssertionExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ bool SequenceConcatExpr::admitsEmptyImpl() const {
if (!it->sequence->admitsEmpty())
return false;

if (it->delay.min == 0 && it->delay.max == 0)
if (it->delay.min == 0u && it->delay.max == 0u)
return false;

if (it->delay.min > 1)
Expand Down
13 changes: 3 additions & 10 deletions source/util/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fmt/format.h>

#include "slang/text/CharInfo.h"
#include "slang/util/OS.h"
#include "slang/util/SmallVector.h"
#include "slang/util/String.h"

Expand Down Expand Up @@ -305,14 +306,6 @@ void CommandLine::parseStr(string_view argList, ParseOptions options, bool& hasA
}

std::string CommandLine::expandVar(const char*& ptr, const char* end) {
auto getEnv = [](const std::string& name) -> std::string {
char* result = getenv(name.c_str());
if (result)
return result;
else
return {};
};

// Three forms for environment variables to try:
// $VAR
// $(VAR)
Expand All @@ -325,7 +318,7 @@ std::string CommandLine::expandVar(const char*& ptr, const char* end) {
while (ptr != end) {
c = *ptr++;
if (c == endDelim)
return getEnv(varName);
return OS::getEnv(varName);

varName += c;
}
Expand All @@ -340,7 +333,7 @@ std::string CommandLine::expandVar(const char*& ptr, const char* end) {
while (ptr != end && isValidCIdChar(*ptr))
varName += *ptr++;

return getEnv(varName);
return OS::getEnv(varName);
}
else {
// This is not a possible variable name so just return what we have.
Expand Down
11 changes: 11 additions & 0 deletions source/util/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "slang/util/OS.h"

#if defined(_MSC_VER)
# pragma warning(disable : 4996) // _CRT_SECURE_NO_WARNINGS
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# include <fcntl.h>
# include <io.h>
Expand Down Expand Up @@ -107,4 +110,12 @@ bool OS::readFile(const fs::path& path, std::vector<char>& buffer) {
return true;
}

std::string OS::getEnv(const std::string& name) {
char* result = getenv(name.c_str());
if (result)
return result;
else
return {};
};

} // namespace slang
6 changes: 4 additions & 2 deletions source/util/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "slang/util/SmallVector.h"

#if defined(_MSC_VER)
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
#endif

Expand Down Expand Up @@ -76,12 +78,12 @@ optional<double> strToDouble(string_view str, size_t* pos) {

void strToUpper(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return std::toupper(c); });
[](unsigned char c) { return (char)std::toupper(c); });
}

void strToLower(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return std::tolower(c); });
[](unsigned char c) { return (char)std::tolower(c); });
}

int editDistance(string_view left, string_view right, bool allowReplacements, int maxDistance) {
Expand Down
2 changes: 2 additions & 0 deletions tests/unittests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "slang/util/BumpAllocator.h"

#if defined(_MSC_VER)
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
#endif

Expand Down
1 change: 0 additions & 1 deletion tools/driver/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ void writeToFile(Stream& os, string_view fileName, String contents) {
}

#if defined(_MSC_VER)
# include <Windows.h>

void writeToFile(string_view fileName, string_view contents) {
if (fileName == "-") {
Expand Down

0 comments on commit 0f9afc9

Please sign in to comment.