Skip to content

Commit

Permalink
update_cmakelists.py no longer overwrites. (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirVer committed Oct 13, 2016
1 parent 7d93ac3 commit 0bf37d0
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions scripts/update_cmakelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
# limitations under the License.
"""A dumb CMakeLists.txt generator that relies on source name conventions."""

import os
from os import path
import re
import argparse
import collections
import os
import re


def MakeRelative(filename, directory):
Expand Down Expand Up @@ -70,7 +71,7 @@ def Format(self, directory):
lines.append(" DEPENDS")
lines.extend(" " + s for s in sorted(self.depends))
lines.append(")")
return "\n".join(lines)
return "\n".join(lines) + "\n\n"


def ExtractProjectIncludes(project_name, source):
Expand Down Expand Up @@ -130,23 +131,41 @@ def FindSourceFiles(basedir):
yield (directory, sources)


def ReadFileWithoutGoogleTargets(filename):
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"
if path.exists(filename):
ignoring = False
for line in open(filename):
if line.startswith("google_"):
m = GOOGLE_TARGET.match(line)
if m is not None:
current_target = m.group(1)
ignoring = True
elif line == ")" and ignoring:
continue
if line == "\n" and ignoring:
ignoring = False
elif not ignoring:
yield line.rstrip()
continue
if not ignoring:
parts[current_target].append(line)
return parts


def ParseArgs():
p = argparse.ArgumentParser(
description="Automatically update cMakeFiles using build conventions.")
p.add_argument("root", type=str, help="Source directory.")
return p.parse_args()

args = p.parse_args()
args.root = args.root.rstrip("/")
return args


def main():
Expand Down Expand Up @@ -226,14 +245,30 @@ def AddTarget(target_type, name, directory, srcs, hdrs):
target.type = "google_catkin_test"

cmake_file = path.join(directory, "CMakeLists.txt")
lines = list(ReadFileWithoutGoogleTargets(cmake_file))
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])

with open(cmake_file, "w") as outfile:
if lines:
outfile.write("\n".join(lines))
outfile.write("\n")
outfile.write("\n\n".join(
t.Format(directory) for t in targets_in_directory))
outfile.write("\n")
outfile.write("".join(sorted_parts).rstrip() + "\n")


if __name__ == "__main__":
Expand Down

0 comments on commit 0bf37d0

Please sign in to comment.