You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

cmake_minimum_required(VERSION 3.20)
project(compiler LANGUAGES C CXX)
# 统一 C++ 标准(按实验环境可调整)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 可执行文件输出目录:统一放到 <build>/bin 下(避免落在 build/src/ 等子目录)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
foreach(cfg IN ITEMS Debug Release RelWithDebInfo MinSizeRel)
string(TOUPPER "${cfg}" cfg_upper)
set("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${cfg_upper}" "${CMAKE_BINARY_DIR}/bin")
endforeach()
# ANTLR 生成代码目录约定(不进仓库,生成在构建目录)
set(ANTLR4_GENERATED_DIR "${CMAKE_BINARY_DIR}/generated/antlr4")
# 统一的编译/包含目录选项(子模块复用)
add_library(build_options INTERFACE)
target_compile_features(build_options INTERFACE cxx_std_17)
target_include_directories(build_options INTERFACE
"${PROJECT_SOURCE_DIR}/include"
"${PROJECT_SOURCE_DIR}/src"
"${ANTLR4_GENERATED_DIR}"
)
option(COMPILER_ENABLE_WARNINGS "Enable common compiler warnings" ON)
if(COMPILER_ENABLE_WARNINGS)
if(MSVC)
target_compile_options(build_options INTERFACE /W4)
else()
target_compile_options(build_options INTERFACE -Wall -Wextra -Wpedantic)
endif()
endif()
# 尝试查找系统 ANTLR4 runtime找不到则提供占位 target便于先配置工程
find_package(antlr4-runtime CONFIG QUIET)
set(ANTLR4_RUNTIME_TARGET "")
if(TARGET antlr4_shared)
set(ANTLR4_RUNTIME_TARGET antlr4_shared)
elseif(TARGET antlr4_static)
set(ANTLR4_RUNTIME_TARGET antlr4_static)
elseif(TARGET antlr4-runtime)
set(ANTLR4_RUNTIME_TARGET antlr4-runtime)
endif()
if(ANTLR4_RUNTIME_TARGET STREQUAL "")
add_library(antlr4_runtime_fallback INTERFACE)
set(ANTLR4_RUNTIME_TARGET antlr4_runtime_fallback)
message(STATUS "antlr4-runtime 未找到:将以占位 target 配置工程;需要 ANTLR 功能时请安装/引入 antlr4-runtime。")
endif()
add_subdirectory(src)