cleanup: generate a cleaner CMake file

This simplifies the CMake code to ask for the minimum required version and also allow newer policies.
It also uses `target_include_directories()` to set the header search path in each library (and their downstream dependencies).  Using `include_directories()` is not idiomatic in CMake >= 3.0.  It sets the include path for all targets and one may need to have a few targets with a different search path.

PiperOrigin-RevId: 538450541
pull/13171/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 34c692f0f9
commit e83207ab8b
  1. 49
      cmake/make_cmakelists.py

@ -45,6 +45,19 @@ def StripFirstChar(deps):
def IsSourceFile(name):
return name.endswith(".c") or name.endswith(".cc")
ADD_LIBRARY_FORMAT = """
add_library(%(name)s %(type)s
%(sources)s
)
target_include_directories(%(name)s %(keyword)s
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../cmake>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINRARY_DIR}>
)
"""
class BuildFileFunctions(object):
def __init__(self, converter):
self.converter = converter
@ -60,7 +73,7 @@ class BuildFileFunctions(object):
def load(self, *args):
pass
def cc_library(self, **kwargs):
if kwargs["name"].endswith("amalgamation"):
return
@ -83,18 +96,23 @@ class BuildFileFunctions(object):
if list(filter(IsSourceFile, files)):
# Has sources, make this a normal library.
self.converter.toplevel += "add_library(%s\n %s)\n" % (
kwargs["name"],
"\n ".join(found_files)
)
self.converter.toplevel += ADD_LIBRARY_FORMAT % {
"name": kwargs["name"],
"type": "",
"keyword": "PUBLIC",
"sources": "\n ".join(found_files),
}
self._add_deps(kwargs)
else:
# Header-only library, have to do a couple things differently.
# For some info, see:
# http://mariobadr.com/creating-a-header-only-library-with-cmake.html
self.converter.toplevel += "add_library(%s INTERFACE)\n" % (
kwargs["name"]
)
self.converter.toplevel += ADD_LIBRARY_FORMAT % {
"name": kwargs["name"],
"type": "INTERFACE",
"keyword": "INTERFACE",
"sources": "",
}
self._add_deps(kwargs, " INTERFACE")
def cc_binary(self, **kwargs):
@ -298,16 +316,7 @@ class Converter(object):
template = textwrap.dedent("""\
# This file was generated from BUILD using tools/make_cmakelists.py.
cmake_minimum_required(VERSION 3.1)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.12)
endif()
cmake_minimum_required (VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
cmake_minimum_required(VERSION 3.10...3.24)
%(prelude)s
@ -345,10 +354,6 @@ class Converter(object):
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()
include_directories(..)
include_directories(../cmake)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(NOT TARGET utf8_range)
if(EXISTS ../external/utf8_range)
# utf8_range is already installed

Loading…
Cancel
Save