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"""
35from __future__ import print_function
36
37import sys
38import os
39import subprocess
40import argparse
41
42def get_android_build_top():
43  path_to_top = os.environ.get('ANDROID_BUILD_TOP')
44  if not path_to_top:
45    # nothing set. try to guess it based on the relative path of this env.py file.
46    this_file_path = os.path.realpath(__file__)
47    path_to_top = os.path.join(os.path.dirname(this_file_path), '../..')
48    path_to_top = os.path.realpath(path_to_top)
49
50  if not os.path.exists(os.path.join(path_to_top, 'build/envsetup.sh')):
51    print(path_to_top)
52    raise AssertionError("geneate_cmake_lists.py must be located inside an android source tree")
53
54  return path_to_top
55
56def main():
57  # Parse arguments
58  parser = argparse.ArgumentParser(description="Generate CMakeLists files for ART")
59  parser.add_argument('--project-name', dest="project_name", required=True,
60                      help='name of the project')
61  parser.add_argument('--arch', dest="arch", required=True, help='arch')
62  args = parser.parse_args()
63  project_name = args.project_name
64  arch = args.arch
65
66  # Invoke make to generate CMakeFiles
67  os.environ['SOONG_GEN_CMAKEFILES']='1'
68  os.environ['SOONG_GEN_CMAKEFILES_DEBUG']='1'
69
70  ANDROID_BUILD_TOP = get_android_build_top()
71
72  subprocess.check_output('build/soong/soong_ui.bash --make-mode', shell=True, cwd=ANDROID_BUILD_TOP)
73
74  out_art_cmakelists_dir = os.path.join(ANDROID_BUILD_TOP,
75                                        'out/development/ide/clion/art')
76
77  # Prepare a list of directories containing generated CMakeLists files for sub projects.
78  cmake_sub_dirs = set()
79  for root, dirs, files in os.walk(out_art_cmakelists_dir):
80    for name in files:
81      if name == 'CMakeLists.txt':
82        if (os.path.samefile(root, out_art_cmakelists_dir)):
83          continue
84        if arch not in root:
85          continue
86        cmake_sub_dir = cmake_sub_dirs.add(root.replace(out_art_cmakelists_dir,
87                                                        '.'))
88
89  # Generate CMakeLists file.
90  f = open(os.path.join(out_art_cmakelists_dir, 'CMakeLists.txt'), 'w')
91  f.write('cmake_minimum_required(VERSION 3.6)\n')
92  f.write('project(%s)\n' % (project_name))
93
94  for dr in cmake_sub_dirs:
95    f.write('add_subdirectory(%s)\n' % (dr))
96
97
98if __name__ == '__main__':
99  main()
100