Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_info.py « cmake « build_files - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f12086c032533ff98b0446070ad5579b551252d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3

# SPDX-License-Identifier: GPL-2.0-or-later

# <pep8 compliant>

"""
Module for accessing project file data for Blender.

Before use, call init(cmake_build_dir).
"""

# TODO: Use CMAKE_EXPORT_COMPILE_COMMANDS (compile_commands.json)
# Instead of Eclipse project format.

__all__ = (
    "SIMPLE_PROJECTFILE",
    "SOURCE_DIR",
    "CMAKE_DIR",
    "PROJECT_DIR",
    "source_list",
    "is_project_file",
    "is_c_header",
    "is_py",
    "cmake_advanced_info",
    "cmake_compiler_defines",
    "project_name_get",
    "init",
)

from typing import (
    Callable,
    Generator,
    List,
    Optional,
    Union,
    Tuple,
)


import sys
if sys.version_info.major < 3:
    print("\nPython3.x needed, found %s.\nAborting!\n" %
          sys.version.partition(" ")[0])
    sys.exit(1)


import subprocess
import os
from os.path import (
    abspath,
    dirname,
    exists,
    join,
    normpath,
    splitext,
)

SOURCE_DIR = join(dirname(__file__), "..", "..")
SOURCE_DIR = normpath(SOURCE_DIR)
SOURCE_DIR = abspath(SOURCE_DIR)

SIMPLE_PROJECTFILE = False

# must initialize from 'init'
CMAKE_DIR = ""
PROJECT_DIR = ""


def init(cmake_path: str) -> bool:
    global CMAKE_DIR, PROJECT_DIR

    # get cmake path
    cmake_path = cmake_path or ""

    if (not cmake_path) or (not exists(join(cmake_path, "CMakeCache.txt"))):
        cmake_path = os.getcwd()
    if not exists(join(cmake_path, "CMakeCache.txt")):
        print("CMakeCache.txt not found in %r or %r\n"
              "    Pass CMake build dir as an argument, or run from that dir, aborting" %
              (cmake_path, os.getcwd()))
        return False

    PROJECT_DIR = CMAKE_DIR = cmake_path
    return True


def source_list(
        path: str,
        filename_check: Optional[Callable[[str], bool]] = None,
) -> Generator[str, None, None]:
    for dirpath, dirnames, filenames in os.walk(path):
        # skip '.git'
        dirnames[:] = [d for d in dirnames if not d.startswith(".")]

        for filename in filenames:
            filepath = join(dirpath, filename)
            if filename_check is None or filename_check(filepath):
                yield filepath


# extension checking
def is_cmake(filename: str) -> bool:
    ext = splitext(filename)[1]
    return (ext == ".cmake") or (filename.endswith("CMakeLists.txt"))


def is_c_header(filename: str) -> bool:
    ext = splitext(filename)[1]
    return (ext in {".h", ".hpp", ".hxx", ".hh"})


def is_py(filename: str) -> bool:
    ext = splitext(filename)[1]
    return (ext == ".py")


def is_glsl(filename: str) -> bool:
    ext = splitext(filename)[1]
    return (ext == ".glsl")


def is_c(filename: str) -> bool:
    ext = splitext(filename)[1]
    return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl", ".osl"})


def is_c_any(filename: str) -> bool:
    return is_c(filename) or is_c_header(filename)


def is_svn_file(filename: str) -> bool:
    dn, fn = os.path.split(filename)
    filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
    return exists(filename_svn)


def is_project_file(filename: str) -> bool:
    return (is_c_any(filename) or is_cmake(filename) or is_glsl(filename))  # and is_svn_file(filename)


def cmake_advanced_info() -> Union[Tuple[List[str], List[Tuple[str, str]]], Tuple[None, None]]:
    """ Extract includes and defines from cmake.
    """

    make_exe = cmake_cache_var("CMAKE_MAKE_PROGRAM")
    if make_exe is None:
        print("Make command not found in: %r not found" % project_path)
        return None, None

    make_exe_basename = os.path.basename(make_exe)

    def create_eclipse_project() -> str:
        print("CMAKE_DIR %r" % CMAKE_DIR)
        if sys.platform == "win32":
            raise Exception("Error: win32 is not supported")
        else:
            if make_exe_basename.startswith(("make", "gmake")):
                cmd = ("cmake", CMAKE_DIR, "-GEclipse CDT4 - Unix Makefiles")
            elif make_exe_basename.startswith("ninja"):
                cmd = ("cmake", CMAKE_DIR, "-GEclipse CDT4 - Ninja")
            else:
                raise Exception("Unknown make program %r" % make_exe)

        subprocess.check_call(cmd)
        return join(CMAKE_DIR, ".cproject")

    includes = []
    defines = []

    project_path = create_eclipse_project()

    if not exists(project_path):
        print("Generating Eclipse Prokect File Failed: %r not found" % project_path)
        return None, None

    from xml.dom.minidom import parse
    tree = parse(project_path)

    # to check on nicer xml
    # f = open(".cproject_pretty", 'w')
    # f.write(tree.toprettyxml(indent="    ", newl=""))

    ELEMENT_NODE = tree.ELEMENT_NODE

    cproject, = tree.getElementsByTagName("cproject")
    for storage in cproject.childNodes:
        if storage.nodeType != ELEMENT_NODE:
            continue

        if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
            cconfig = storage.getElementsByTagName("cconfiguration")[0]
            for substorage in cconfig.childNodes:
                if substorage.nodeType != ELEMENT_NODE:
                    continue

                moduleId = substorage.attributes["moduleId"].value

                # org.eclipse.cdt.core.settings
                # org.eclipse.cdt.core.language.mapping
                # org.eclipse.cdt.core.externalSettings
                # org.eclipse.cdt.core.pathentry
                # org.eclipse.cdt.make.core.buildtargets

                if moduleId == "org.eclipse.cdt.core.pathentry":
                    for path in substorage.childNodes:
                        if path.nodeType != ELEMENT_NODE:
                            continue
                        kind = path.attributes["kind"].value

                        if kind == "mac":
                            # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
                            defines.append((path.attributes["name"].value, path.attributes["value"].value))
                        elif kind == "inc":
                            # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
                            includes.append(path.attributes["include"].value)
                        else:
                            pass

    return includes, defines


def cmake_cache_var(var: str) -> Optional[str]:
    with open(os.path.join(CMAKE_DIR, "CMakeCache.txt"), encoding='utf-8') as cache_file:
        lines = [
            l_strip for l in cache_file
            if (l_strip := l.strip())
            if not l_strip.startswith(("//", "#"))
        ]

    for l in lines:
        if l.split(":")[0] == var:
            return l.split("=", 1)[-1]
    return None


def cmake_compiler_defines() -> Optional[List[str]]:
    compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too

    if compiler is None:
        print("Couldn't find the compiler, os defines will be omitted...")
        return None

    import tempfile
    temp_c = tempfile.mkstemp(suffix=".c")[1]
    temp_def = tempfile.mkstemp(suffix=".def")[1]

    os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))

    temp_def_file = open(temp_def)
    lines = [l.strip() for l in temp_def_file if l.strip()]
    temp_def_file.close()

    os.remove(temp_c)
    os.remove(temp_def)
    return lines


def project_name_get() -> Optional[str]:
    return cmake_cache_var("CMAKE_PROJECT_NAME")