#!/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.""" import os from os import path import re 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(")") return "\n".join(lines) def ExtractCartographerIncludes(source): """Returns all locally included files.""" includes = set() for line in open(MaybeUseCmakeFile(source)): if source.endswith(".proto"): match = re.match(r'^import "(cartographer/[^"]+)', line) else: match = re.match(r'^#include "(cartographer/[^"]+)"', line) if match: includes.add(match.group(1)) return includes def ExtractUses(source): """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 ", line): uses.add("USES_LUA") if re.match(r'^#include "ceres/', line): uses.add("USES_CERES") if re.match(r'^#include "glog/', line): uses.add("USES_GLOG") if re.match(r'^#include "gflags/', line): uses.add("USES_GFLAGS") if re.match(r"^#include