]> granicus.if.org Git - esp-idf/blob - tools/cmake/convert_to_cmake.py
Merge branch 'bugfix/btdm_watchdog_timeout_after_pair_with_iphone' into 'master'
[esp-idf] / tools / cmake / convert_to_cmake.py
1 #!/usr/bin/env python
2 #
3 # Command line tool to convert simple ESP-IDF Makefile & component.mk files to
4 # CMakeLists.txt files
5 #
6 import argparse
7 import subprocess
8 import re
9 import os.path
10 import glob
11 import sys
12
13 debug = False
14
15 def get_make_variables(path, makefile="Makefile", expected_failure=False, variables={}):
16     """
17     Given the path to a Makefile of some kind, return a dictionary of all variables defined in this Makefile
18
19     Uses 'make' to parse the Makefile syntax, so we don't have to!
20
21     Overrides IDF_PATH= to avoid recursively evaluating the entire project Makefile structure.
22     """
23     variable_setters = [ ("%s=%s" % (k,v)) for (k,v) in variables.items() ]
24
25     cmdline = ["make", "-rpn", "-C", path, "-f", makefile ] + variable_setters
26     if debug:
27         print("Running %s..." % (" ".join(cmdline)))
28
29     p = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30     (output, stderr) = p.communicate("\n")
31
32     if (not expected_failure) and p.returncode != 0:
33         raise RuntimeError("Unexpected make failure, result %d" % p.returncode)
34
35     if debug:
36         print("Make stdout:")
37         print(output)
38         print("Make stderr:")
39         print(stderr)
40
41     next_is_makefile = False  # is the next line a makefile variable?
42     result = {}
43     BUILT_IN_VARS = set(["MAKEFILE_LIST", "SHELL", "CURDIR", "MAKEFLAGS"])
44
45     for line in output.decode().split("\n"):
46         if line.startswith("# makefile"):  # this line appears before any variable defined in the makefile itself
47             next_is_makefile = True
48         elif next_is_makefile:
49             next_is_makefile = False
50             m = re.match(r"(?P<var>[^ ]+) :?= (?P<val>.+)", line)
51             if m is not None:
52                 if not m.group("var") in BUILT_IN_VARS:
53                     result[m.group("var")] = m.group("val").strip()
54
55     return result
56
57 def get_component_variables(project_path, component_path):
58     make_vars = get_make_variables(component_path,
59                                    os.path.join(os.environ["IDF_PATH"],
60                                                 "make",
61                                                 "component_wrapper.mk"),
62                                    expected_failure=True,
63                                    variables = {
64                                        "COMPONENT_MAKEFILE" : os.path.join(component_path, "component.mk"),
65                                        "COMPONENT_NAME" : os.path.basename(component_path),
66                                        "PROJECT_PATH": project_path,
67                                    })
68
69     if "COMPONENT_OBJS" in make_vars:  # component.mk specifies list of object files
70         # Convert to sources
71         def find_src(obj):
72             obj = os.path.splitext(obj)[0]
73             for ext in [ "c", "cpp", "S" ]:
74                 if os.path.exists(os.path.join(component_path, obj) + "." + ext):
75                     return obj + "." + ext
76             print("WARNING: Can't find source file for component %s COMPONENT_OBJS %s" % (component_path, obj))
77             return None
78
79         srcs = []
80         for obj in make_vars["COMPONENT_OBJS"].split(" "):
81             src = find_src(obj)
82             if src is not None:
83                 srcs.append(src)
84         make_vars["COMPONENT_SRCS"] = " ".join(srcs)
85     else:
86         component_srcs = list()
87         for component_srcdir in make_vars.get("COMPONENT_SRCDIRS", ".").split(" "):
88             component_srcdir_path = os.path.abspath(os.path.join(component_path, component_srcdir))
89             
90             srcs = list()
91             srcs += glob.glob(os.path.join(component_srcdir_path, "*.[cS]"))
92             srcs += glob.glob(os.path.join(component_srcdir_path, "*.cpp"))
93             srcs = [('"%s"' % str(os.path.relpath(s, component_path))) for s in srcs]
94
95     make_vars["COMPONENT_ADD_INCLUDEDIRS"] = make_vars.get("COMPONENT_ADD_INCLUDEDIRS", "include")
96             component_srcs += srcs
97         make_vars["COMPONENT_SRCS"] = " ".join(component_srcs)
98
99
100     return make_vars
101
102
103 def convert_project(project_path):
104     if not os.path.exists(project_path):
105         raise RuntimeError("Project directory '%s' not found" % project_path)
106     if not os.path.exists(os.path.join(project_path, "Makefile")):
107         raise RuntimeError("Directory '%s' doesn't contain a project Makefile" % project_path)
108
109     project_cmakelists = os.path.join(project_path, "CMakeLists.txt")
110     if os.path.exists(project_cmakelists):
111         raise RuntimeError("This project already has a CMakeLists.txt file")
112
113     project_vars = get_make_variables(project_path, expected_failure=True)
114     if not "PROJECT_NAME" in project_vars:
115         raise RuntimeError("PROJECT_NAME does not appear to be defined in IDF project Makefile at %s" % project_path)
116
117     component_paths = project_vars["COMPONENT_PATHS"].split(" ")
118
119     # Convert components as needed
120     for p in component_paths:
121         convert_component(project_path, p)
122
123     project_name = project_vars["PROJECT_NAME"]
124
125     # Generate the project CMakeLists.txt file
126     with open(project_cmakelists, "w") as f:
127         f.write("""
128 # (Automatically converted from project Makefile by convert_to_cmake.py.)
129
130 # The following lines of boilerplate have to be in your project's CMakeLists
131 # in this exact order for cmake to work correctly
132 cmake_minimum_required(VERSION 3.5)
133
134 """)
135         f.write("""
136 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
137 """)
138         f.write("project(%s)\n" % project_name)
139
140     print("Converted project %s" % project_cmakelists)
141
142 def convert_component(project_path, component_path):
143     if debug:
144         print("Converting %s..." % (component_path))
145     cmakelists_path = os.path.join(component_path, "CMakeLists.txt")
146     if os.path.exists(cmakelists_path):
147         print("Skipping already-converted component %s..." % cmakelists_path)
148         return
149     v = get_component_variables(project_path, component_path)
150
151     # Look up all the variables before we start writing the file, so it's not
152     # created if there's an erro
153     component_srcs = v.get("COMPONENT_SRCS", None)
154
155     component_add_includedirs = v["COMPONENT_ADD_INCLUDEDIRS"]
156     cflags = v.get("CFLAGS", None)
157
158     with open(cmakelists_path, "w") as f:
159         f.write("set(COMPONENT_ADD_INCLUDEDIRS %s)\n\n" % component_add_includedirs)
160
161         f.write("# Edit following two lines to set component requirements (see docs)\n")
162         f.write("set(COMPONENT_REQUIRES "")\n")
163         f.write("set(COMPONENT_PRIV_REQUIRES "")\n\n")
164
165         if component_srcs is not None:
166             f.write("set(COMPONENT_SRCS %s)\n\n" % component_srcs)
167             f.write("register_component()\n")
168         else:
169             f.write("register_config_only_component()\n")
170         if cflags is not None:
171             f.write("component_compile_options(%s)\n" % cflags)
172
173     print("Converted %s" % cmakelists_path)
174
175
176 def main():
177     global debug
178
179     parser = argparse.ArgumentParser(description='convert_to_cmake.py - ESP-IDF Project Makefile to CMakeLists.txt converter', prog='convert_to_cmake')
180
181     parser.add_argument('--debug', help='Display debugging output',
182                         action='store_true')
183
184     parser.add_argument('project', help='Path to project to convert (defaults to CWD)', default=os.getcwd(), metavar='project path', nargs='?')
185
186     args = parser.parse_args()
187     debug = args.debug
188     print("Converting %s..." % args.project)
189     convert_project(args.project)
190
191
192 if __name__ == "__main__":
193     main()