2016-08-02 15:07:31 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2016 The Cartographer Authors
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""A dumb CMakeLists.txt generator that relies on source name conventions."""
|
|
|
|
|
|
|
|
from os import path
|
2016-10-12 23:03:46 +08:00
|
|
|
import argparse
|
2016-10-13 21:30:44 +08:00
|
|
|
import collections
|
|
|
|
import os
|
|
|
|
import re
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
def MakeRelative(filename, directory):
|
|
|
|
absolute_directory = path.realpath(directory)
|
|
|
|
filename = path.realpath(filename)
|
|
|
|
return path.relpath(filename, absolute_directory)
|
|
|
|
|
|
|
|
|
|
|
|
def MaybeUseCmakeFile(filename):
|
|
|
|
cmake_filename = filename + ".cmake"
|
|
|
|
return cmake_filename if os.path.exists(cmake_filename) else filename
|
|
|
|
|
|
|
|
|
|
|
|
class Target(object):
|
|
|
|
"""Container for data for a cmake target."""
|
|
|
|
|
|
|
|
def __init__(self, target_type, name, directory, srcs, hdrs):
|
|
|
|
self.type = target_type
|
|
|
|
self.name = name
|
|
|
|
self.directory = directory
|
|
|
|
self.srcs = srcs
|
|
|
|
self.hdrs = hdrs
|
|
|
|
self.depends = set()
|
|
|
|
self.uses = set()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "%s(%s, nsrcs:%s, %s)" % (self.type, self.name, len(self.srcs),
|
|
|
|
self.depends)
|
|
|
|
|
|
|
|
def Format(self, directory):
|
|
|
|
"""Formats the target for writing into a CMakeLists.txt file."""
|
|
|
|
lines = ["%s(%s" % (self.type, self.name),]
|
|
|
|
|
|
|
|
for use in sorted(self.uses):
|
|
|
|
lines.append(" " + use)
|
|
|
|
|
|
|
|
if self.srcs:
|
|
|
|
lines.append(" SRCS")
|
|
|
|
lines.extend(" " + MakeRelative(s, directory)
|
|
|
|
for s in sorted(self.srcs))
|
|
|
|
|
|
|
|
if self.hdrs:
|
|
|
|
lines.append(" HDRS")
|
|
|
|
lines.extend(" " + MakeRelative(s, directory)
|
|
|
|
for s in sorted(self.hdrs))
|
|
|
|
|
|
|
|
if self.depends:
|
|
|
|
lines.append(" DEPENDS")
|
|
|
|
lines.extend(" " + s for s in sorted(self.depends))
|
|
|
|
lines.append(")")
|
2016-10-13 21:30:44 +08:00
|
|
|
return "\n".join(lines) + "\n\n"
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
|
2016-10-12 23:03:46 +08:00
|
|
|
def ExtractProjectIncludes(project_name, source):
|
2016-08-02 15:07:31 +08:00
|
|
|
"""Returns all locally included files."""
|
|
|
|
includes = set()
|
|
|
|
for line in open(MaybeUseCmakeFile(source)):
|
|
|
|
if source.endswith(".proto"):
|
2016-10-12 23:03:46 +08:00
|
|
|
match = re.match(r'^import "(' + project_name + r'/[^"]+)', line)
|
2016-08-02 15:07:31 +08:00
|
|
|
else:
|
2016-10-12 23:03:46 +08:00
|
|
|
match = re.match(r'^#include "(' + project_name + r'/[^"]+)"', line)
|
2016-08-02 15:07:31 +08:00
|
|
|
if match:
|
|
|
|
includes.add(match.group(1))
|
|
|
|
return includes
|
|
|
|
|
|
|
|
|
2016-10-12 23:03:46 +08:00
|
|
|
def ExtractUses(project_name, source):
|
2016-08-02 15:07:31 +08:00
|
|
|
"""Finds the options for the third_party libraries used."""
|
|
|
|
uses = set()
|
|
|
|
for line in open(MaybeUseCmakeFile(source)):
|
|
|
|
if re.match(r'^#include "Eigen/', line):
|
|
|
|
uses.add("USES_EIGEN")
|
|
|
|
if re.match(r"^#include <lua.hpp>", line):
|
|
|
|
uses.add("USES_LUA")
|
2016-10-11 21:18:59 +08:00
|
|
|
if re.match(r'^#include "ceres/', line):
|
2016-08-02 15:07:31 +08:00
|
|
|
uses.add("USES_CERES")
|
2016-10-11 21:18:59 +08:00
|
|
|
if re.match(r'^#include "glog/', line):
|
|
|
|
uses.add("USES_GLOG")
|
|
|
|
if re.match(r'^#include "gflags/', line):
|
|
|
|
uses.add("USES_GFLAGS")
|
2016-10-12 23:03:46 +08:00
|
|
|
if re.match(r'^#include ["<]boost/', line):
|
2016-08-02 15:07:31 +08:00
|
|
|
uses.add("USES_BOOST")
|
2016-10-12 23:03:46 +08:00
|
|
|
if re.match(r'^#include ["<]webp/', line):
|
2016-08-02 15:07:31 +08:00
|
|
|
uses.add("USES_WEBP")
|
2016-10-12 23:03:46 +08:00
|
|
|
if re.match(r'^#include ["<]pcl/', line):
|
|
|
|
uses.add("USES_PCL")
|
|
|
|
if re.match(r'^#include ["<]ros/', line):
|
|
|
|
uses.add("USES_ROS")
|
|
|
|
if re.match(r'^#include "[a-zA-Z]*_msgs/', line):
|
|
|
|
uses.add("USES_ROS")
|
|
|
|
if re.match(r'^#include ["<]yaml-cpp/', line):
|
|
|
|
uses.add("USES_YAMLCPP")
|
2016-10-14 21:14:46 +08:00
|
|
|
if re.match(r'^#include ["<]cairo/', line):
|
|
|
|
uses.add("USES_CAIRO")
|
2016-10-12 23:03:46 +08:00
|
|
|
if project_name != "cartographer":
|
|
|
|
if re.match(r'^#include ["<]cartographer/', line):
|
|
|
|
uses.add("USES_CARTOGRAPHER")
|
2016-08-02 15:07:31 +08:00
|
|
|
return uses
|
|
|
|
|
|
|
|
|
|
|
|
def FindSourceFiles(basedir):
|
|
|
|
sources = set()
|
|
|
|
for (directory, _, filenames) in os.walk(basedir):
|
|
|
|
for filename in filenames:
|
|
|
|
ext = path.splitext(filename)[-1]
|
|
|
|
if ext in [".h", ".cc", ".proto"]:
|
|
|
|
sources.add(path.join(directory, filename))
|
|
|
|
elif filename.endswith(".h.cmake"):
|
|
|
|
sources.add(path.join(directory, path.splitext(filename)[0]))
|
|
|
|
yield (directory, sources)
|
|
|
|
|
|
|
|
|
2016-10-13 21:30:44 +08:00
|
|
|
def GetNonGoogleTargetLines(filename):
|
|
|
|
"""Returns text not written by this script.
|
|
|
|
|
|
|
|
Returns a dictionary where keys are target names and values are list of
|
|
|
|
lines that came after this target in the file. It also contains a special key
|
|
|
|
called 'START'
|
|
|
|
for lines that came before any target.
|
|
|
|
"""
|
|
|
|
GOOGLE_TARGET = re.compile(r"^google_[a-z_]*\((.*)$")
|
|
|
|
parts = collections.defaultdict(list)
|
|
|
|
current_target = "START"
|
2016-08-02 15:07:31 +08:00
|
|
|
if path.exists(filename):
|
|
|
|
ignoring = False
|
|
|
|
for line in open(filename):
|
2016-10-13 21:30:44 +08:00
|
|
|
m = GOOGLE_TARGET.match(line)
|
|
|
|
if m is not None:
|
|
|
|
current_target = m.group(1)
|
2016-08-02 15:07:31 +08:00
|
|
|
ignoring = True
|
2016-10-13 21:30:44 +08:00
|
|
|
continue
|
|
|
|
if line == "\n" and ignoring:
|
2016-08-02 15:07:31 +08:00
|
|
|
ignoring = False
|
2016-10-13 21:30:44 +08:00
|
|
|
continue
|
|
|
|
if not ignoring:
|
|
|
|
parts[current_target].append(line)
|
|
|
|
return parts
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
|
2016-10-12 23:03:46 +08:00
|
|
|
def ParseArgs():
|
|
|
|
p = argparse.ArgumentParser(
|
|
|
|
description="Automatically update cMakeFiles using build conventions.")
|
2016-10-14 19:43:09 +08:00
|
|
|
p.add_argument("root", type=str, nargs="*", help="Source directory.")
|
2016-10-13 21:30:44 +08:00
|
|
|
|
|
|
|
args = p.parse_args()
|
|
|
|
return args
|
2016-10-12 23:03:46 +08:00
|
|
|
|
|
|
|
|
2016-08-02 15:07:31 +08:00
|
|
|
def main():
|
2016-10-12 23:03:46 +08:00
|
|
|
args = ParseArgs()
|
2016-10-14 19:43:09 +08:00
|
|
|
|
|
|
|
for root in args.root:
|
|
|
|
RunOnDirectory(root)
|
|
|
|
|
|
|
|
|
|
|
|
def RunOnDirectory(root):
|
|
|
|
root = root.rstrip("/")
|
2016-08-02 15:07:31 +08:00
|
|
|
targets_by_src = {}
|
|
|
|
targets = []
|
|
|
|
|
2016-10-14 19:43:09 +08:00
|
|
|
project_name = os.path.basename(root)
|
|
|
|
base_directory = path.realpath(path.join(root, path.pardir))
|
2016-08-02 15:07:31 +08:00
|
|
|
directories = set()
|
|
|
|
|
|
|
|
def AddTarget(target_type, name, directory, srcs, hdrs):
|
|
|
|
"""Adds a new target to 'targets' and updates 'targets_by_src'."""
|
|
|
|
target = Target(target_type, name, directory, srcs, hdrs)
|
|
|
|
targets.append(target)
|
|
|
|
for s in srcs + hdrs:
|
|
|
|
relative_s = MakeRelative(s, base_directory)
|
|
|
|
targets_by_src[relative_s] = target
|
|
|
|
if s.endswith(".proto"):
|
|
|
|
proto_stem = os.path.splitext(relative_s)[0]
|
|
|
|
targets_by_src[proto_stem + ".pb.h"] = target
|
|
|
|
targets_by_src[proto_stem + ".pb.cc"] = target
|
|
|
|
directories.add(directory)
|
|
|
|
|
2016-10-14 19:43:09 +08:00
|
|
|
for (directory, sources) in FindSourceFiles(root):
|
|
|
|
module_name = path.relpath(directory, path.realpath(root)).replace("/", "_")
|
2016-10-12 23:03:46 +08:00
|
|
|
|
|
|
|
prepend_module_name = lambda s: (module_name + "_" + s) if module_name != "." else s
|
2016-08-02 15:07:31 +08:00
|
|
|
headers = set(fn for fn in sources if fn.endswith(".h"))
|
|
|
|
sources -= headers
|
|
|
|
for h in sorted(headers):
|
|
|
|
srcs = []
|
2016-10-12 23:03:46 +08:00
|
|
|
name = prepend_module_name(path.basename(path.splitext(h)[0]))
|
2016-08-02 15:07:31 +08:00
|
|
|
cc_file = path.splitext(h)[0] + ".cc"
|
|
|
|
if cc_file in sources:
|
|
|
|
sources.remove(cc_file)
|
|
|
|
srcs.append(cc_file)
|
|
|
|
AddTarget("google_library", name, directory, srcs, [h])
|
|
|
|
|
|
|
|
tests = set(fn for fn in sources if fn.endswith("_test.cc"))
|
|
|
|
sources -= tests
|
|
|
|
for c in sorted(tests):
|
2016-10-12 23:03:46 +08:00
|
|
|
name = prepend_module_name(path.basename(path.splitext(c)[0]))
|
2016-08-02 15:07:31 +08:00
|
|
|
AddTarget("google_test", name, directory, [c], [])
|
|
|
|
|
|
|
|
protos = set(fn for fn in sources if fn.endswith(".proto"))
|
|
|
|
sources -= protos
|
|
|
|
for c in sorted(protos):
|
2016-10-12 23:03:46 +08:00
|
|
|
name = prepend_module_name(path.basename(path.splitext(c)[0]))
|
2016-08-02 15:07:31 +08:00
|
|
|
AddTarget("google_proto_library", name, directory, [c], [])
|
|
|
|
|
2016-10-11 21:18:59 +08:00
|
|
|
mains = set(fn for fn in sources if fn.endswith("_main.cc"))
|
|
|
|
sources -= mains
|
|
|
|
for c in sorted(mains):
|
|
|
|
# Binaries do not get their full subpath appended, but we prepend
|
|
|
|
# 'cartographer' to distinguish them after installation. So,
|
2016-10-25 18:27:58 +08:00
|
|
|
# 'io/assets_writer_main.cc' will generate a binary called
|
|
|
|
# 'cartographer_assets_writer'.
|
2016-10-12 23:03:46 +08:00
|
|
|
name = "cartographer_" + path.basename(path.splitext(c)[0][:-5])
|
2016-10-11 21:18:59 +08:00
|
|
|
AddTarget("google_binary", name, directory, [c], [])
|
|
|
|
|
2016-08-02 15:07:31 +08:00
|
|
|
assert (not sources), "Remaining sources without target: %s" % sources
|
|
|
|
|
|
|
|
# Write the CMakeLists.txt files.
|
|
|
|
for directory in directories:
|
|
|
|
targets_in_directory = [t for t in targets if t.directory == directory]
|
|
|
|
for target in targets_in_directory:
|
|
|
|
for src in target.srcs + target.hdrs:
|
2016-10-12 23:03:46 +08:00
|
|
|
for header in ExtractProjectIncludes(project_name, src):
|
2016-08-02 15:07:31 +08:00
|
|
|
dependant = targets_by_src[header]
|
|
|
|
if dependant.name == target.name:
|
|
|
|
continue
|
|
|
|
target.depends.add(dependant.name)
|
2016-10-12 23:03:46 +08:00
|
|
|
target.uses.update(ExtractUses(project_name, src))
|
|
|
|
if target.type is "google_test" and "USES_ROS" in target.uses:
|
|
|
|
target.type = "google_catkin_test"
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
cmake_file = path.join(directory, "CMakeLists.txt")
|
2016-10-13 21:30:44 +08:00
|
|
|
parts = GetNonGoogleTargetLines(cmake_file)
|
|
|
|
|
|
|
|
sorted_parts = []
|
|
|
|
|
|
|
|
def dump(lines_as_list):
|
|
|
|
lines = "".join(lines_as_list)
|
|
|
|
if not lines:
|
|
|
|
return
|
|
|
|
sorted_parts.append(lines)
|
|
|
|
|
|
|
|
dump(parts["START"])
|
|
|
|
del parts["START"]
|
|
|
|
|
|
|
|
for target in targets_in_directory:
|
|
|
|
dump(target.Format(directory))
|
|
|
|
if target.name in parts:
|
|
|
|
dump(parts[target.name])
|
|
|
|
del parts[target.name]
|
|
|
|
|
|
|
|
for target in sorted(parts.keys()):
|
|
|
|
dump(parts[target])
|
|
|
|
|
2016-08-02 15:07:31 +08:00
|
|
|
with open(cmake_file, "w") as outfile:
|
2016-10-13 21:30:44 +08:00
|
|
|
outfile.write("".join(sorted_parts).rstrip() + "\n")
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|