xref: /aosp_15_r20/art/tools/generate_cmake_lists.py (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker#!/usr/bin/env python
2*795d594fSAndroid Build Coastguard Worker#
3*795d594fSAndroid Build Coastguard Worker# Copyright 2017, The Android Open Source Project
4*795d594fSAndroid Build Coastguard Worker#
5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*795d594fSAndroid Build Coastguard Worker#
9*795d594fSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*795d594fSAndroid Build Coastguard Worker#
11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*795d594fSAndroid Build Coastguard Worker# limitations under the License.
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker
18*795d594fSAndroid Build Coastguard Worker"""
19*795d594fSAndroid Build Coastguard Worker./generate_cmake_lists.py --project-name <project-name> --arch <arch>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker- project-name - name of the new project
22*795d594fSAndroid Build Coastguard Worker- arch - arch type. make generates seperate CMakeLists files for
23*795d594fSAndroid Build Coastguard Worker         each architecture. To avoid collision in targets, only one of
24*795d594fSAndroid Build Coastguard Worker         them can be included in the super project.
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard WorkerThe primary objective of this file is to generate CMakeLists files
27*795d594fSAndroid Build Coastguard Workerfor CLion setup.
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard WorkerSteps to setup CLion.
30*795d594fSAndroid Build Coastguard Worker1) Open the generated CMakeList file in CLion as a project.
31*795d594fSAndroid Build Coastguard Worker2) Change the project root ANDROID_BUILD_TOP.
32*795d594fSAndroid Build Coastguard Worker(Also, exclude projects that you don't bother about. This will make
33*795d594fSAndroid Build Coastguard Workerthe indexing faster).
34*795d594fSAndroid Build Coastguard Worker"""
35*795d594fSAndroid Build Coastguard Workerfrom __future__ import print_function
36*795d594fSAndroid Build Coastguard Worker
37*795d594fSAndroid Build Coastguard Workerimport sys
38*795d594fSAndroid Build Coastguard Workerimport os
39*795d594fSAndroid Build Coastguard Workerimport subprocess
40*795d594fSAndroid Build Coastguard Workerimport argparse
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Workerdef get_android_build_top():
43*795d594fSAndroid Build Coastguard Worker  path_to_top = os.environ.get('ANDROID_BUILD_TOP')
44*795d594fSAndroid Build Coastguard Worker  if not path_to_top:
45*795d594fSAndroid Build Coastguard Worker    # nothing set. try to guess it based on the relative path of this env.py file.
46*795d594fSAndroid Build Coastguard Worker    this_file_path = os.path.realpath(__file__)
47*795d594fSAndroid Build Coastguard Worker    path_to_top = os.path.join(os.path.dirname(this_file_path), '../..')
48*795d594fSAndroid Build Coastguard Worker    path_to_top = os.path.realpath(path_to_top)
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker  if not os.path.exists(os.path.join(path_to_top, 'build/envsetup.sh')):
51*795d594fSAndroid Build Coastguard Worker    print(path_to_top)
52*795d594fSAndroid Build Coastguard Worker    raise AssertionError("geneate_cmake_lists.py must be located inside an android source tree")
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker  return path_to_top
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Workerdef main():
57*795d594fSAndroid Build Coastguard Worker  # Parse arguments
58*795d594fSAndroid Build Coastguard Worker  parser = argparse.ArgumentParser(description="Generate CMakeLists files for ART")
59*795d594fSAndroid Build Coastguard Worker  parser.add_argument('--project-name', dest="project_name", required=True,
60*795d594fSAndroid Build Coastguard Worker                      help='name of the project')
61*795d594fSAndroid Build Coastguard Worker  parser.add_argument('--arch', dest="arch", required=True, help='arch')
62*795d594fSAndroid Build Coastguard Worker  args = parser.parse_args()
63*795d594fSAndroid Build Coastguard Worker  project_name = args.project_name
64*795d594fSAndroid Build Coastguard Worker  arch = args.arch
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker  # Invoke make to generate CMakeFiles
67*795d594fSAndroid Build Coastguard Worker  os.environ['SOONG_GEN_CMAKEFILES']='1'
68*795d594fSAndroid Build Coastguard Worker  os.environ['SOONG_GEN_CMAKEFILES_DEBUG']='1'
69*795d594fSAndroid Build Coastguard Worker
70*795d594fSAndroid Build Coastguard Worker  ANDROID_BUILD_TOP = get_android_build_top()
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker  subprocess.check_output('build/soong/soong_ui.bash --make-mode', shell=True, cwd=ANDROID_BUILD_TOP)
73*795d594fSAndroid Build Coastguard Worker
74*795d594fSAndroid Build Coastguard Worker  out_art_cmakelists_dir = os.path.join(ANDROID_BUILD_TOP,
75*795d594fSAndroid Build Coastguard Worker                                        'out/development/ide/clion/art')
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Worker  # Prepare a list of directories containing generated CMakeLists files for sub projects.
78*795d594fSAndroid Build Coastguard Worker  cmake_sub_dirs = set()
79*795d594fSAndroid Build Coastguard Worker  for root, dirs, files in os.walk(out_art_cmakelists_dir):
80*795d594fSAndroid Build Coastguard Worker    for name in files:
81*795d594fSAndroid Build Coastguard Worker      if name == 'CMakeLists.txt':
82*795d594fSAndroid Build Coastguard Worker        if (os.path.samefile(root, out_art_cmakelists_dir)):
83*795d594fSAndroid Build Coastguard Worker          continue
84*795d594fSAndroid Build Coastguard Worker        if arch not in root:
85*795d594fSAndroid Build Coastguard Worker          continue
86*795d594fSAndroid Build Coastguard Worker        cmake_sub_dir = cmake_sub_dirs.add(root.replace(out_art_cmakelists_dir,
87*795d594fSAndroid Build Coastguard Worker                                                        '.'))
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker  # Generate CMakeLists file.
90*795d594fSAndroid Build Coastguard Worker  f = open(os.path.join(out_art_cmakelists_dir, 'CMakeLists.txt'), 'w')
91*795d594fSAndroid Build Coastguard Worker  f.write('cmake_minimum_required(VERSION 3.6)\n')
92*795d594fSAndroid Build Coastguard Worker  f.write('project(%s)\n' % (project_name))
93*795d594fSAndroid Build Coastguard Worker
94*795d594fSAndroid Build Coastguard Worker  for dr in cmake_sub_dirs:
95*795d594fSAndroid Build Coastguard Worker    f.write('add_subdirectory(%s)\n' % (dr))
96*795d594fSAndroid Build Coastguard Worker
97*795d594fSAndroid Build Coastguard Worker
98*795d594fSAndroid Build Coastguard Workerif __name__ == '__main__':
99*795d594fSAndroid Build Coastguard Worker  main()
100