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