diff --git a/CMakeLists.txt b/CMakeLists.txt index 913c5631a..152511525 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,11 @@ option(FLATBUFFERS_BUILD_CPP17 option(FLATBUFFERS_BUILD_LEGACY "Run C++ code generator with '--cpp-std c++0x' switch." OFF) +option(FLATBUFFERS_ENABLE_PCH + "Enable precompile headers support for 'flatbuffers' and 'flatc'. \" + Only work if CMake supports 'target_precompile_headers'. \" + This can speed up compilation time." + OFF) if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) message(WARNING @@ -302,6 +307,15 @@ function(add_fsanitize_to_target _target _sanitizer) endif() endfunction() +function(add_pch_to_target _target _pch_header) + if(COMMAND target_precompile_headers) + target_precompile_headers(${_target} PRIVATE ${_pch_header}) + if(NOT MSVC) + set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON) + endif() + endif() +endfunction() + if(BIICODE) include(biicode/cmake/biicode.cmake) return() @@ -316,10 +330,16 @@ if(FLATBUFFERS_BUILD_FLATLIB) target_include_directories(flatbuffers INTERFACE $) target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}") + if(FLATBUFFERS_ENABLE_PCH) + add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h) + endif() endif() if(FLATBUFFERS_BUILD_FLATC) add_executable(flatc ${FlatBuffers_Compiler_SRCS}) + if(FLATBUFFERS_ENABLE_PCH) + add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h) + endif() target_compile_options(flatc PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}") if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32) add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE}) @@ -352,6 +372,9 @@ if(FLATBUFFERS_BUILD_SHAREDLIB) set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}" VERSION "${FlatBuffers_Library_SONAME_FULL}") + if(FLATBUFFERS_ENABLE_PCH) + add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h) + endif() endif() # Global list of generated files. diff --git a/appveyor.yml b/appveyor.yml index e42fecebd..634a5215c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -28,10 +28,12 @@ environment: - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 CMAKE_VS_VERSION: "15 2017" + CMAKE_OPTIONS: "-DFLATBUFFERS_ENABLE_PCH=1" MONSTER_EXTRA: "" - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 CMAKE_VS_VERSION: "16 2019" + CMAKE_OPTIONS: "-DFLATBUFFERS_ENABLE_PCH=1" MONSTER_EXTRA: "" platform: diff --git a/include/flatbuffers/pch/flatc_pch.h b/include/flatbuffers/pch/flatc_pch.h new file mode 100644 index 000000000..77132790b --- /dev/null +++ b/include/flatbuffers/pch/flatc_pch.h @@ -0,0 +1,39 @@ +/* + * Copyright 2017 Google Inc. 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 FLATBUFFERS_FLATC_PCH_H_ +#define FLATBUFFERS_FLATC_PCH_H_ + +// stl +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// flatbuffers +#include "flatbuffers/pch/pch.h" +#include "flatbuffers/code_generators.h" +#include "flatbuffers/flatbuffers.h" +#include "flatbuffers/flexbuffers.h" +#include "flatbuffers/idl.h" + +#endif // FLATBUFFERS_FLATC_PCH_H_ diff --git a/include/flatbuffers/pch/pch.h b/include/flatbuffers/pch/pch.h new file mode 100644 index 000000000..804e99ed0 --- /dev/null +++ b/include/flatbuffers/pch/pch.h @@ -0,0 +1,38 @@ +/* + * Copyright 2017 Google Inc. 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 FLATBUFFERS_PCH_H_ +#define FLATBUFFERS_PCH_H_ + +// stl +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// flatbuffers +#include "flatbuffers/util.h" + +#endif // FLATBUFFERS_PCH_H_