|  | 
|  | 1 | +import os | 
|  | 2 | +import sys | 
|  | 3 | +import shutil | 
|  | 4 | +import subprocess | 
|  | 5 | +import tempfile | 
|  | 6 | + | 
|  | 7 | +base_path = os.path.abspath(os.path.dirname(__file__)) | 
|  | 8 | + | 
|  | 9 | +project_path = os.path.abspath(os.path.join(base_path, '..', 'lib', 'lvgl')) | 
|  | 10 | +docs_path = os.path.join(project_path, 'docs') | 
|  | 11 | +sys.path.insert(0, docs_path) | 
|  | 12 | + | 
|  | 13 | + | 
|  | 14 | +import create_fake_lib_c # NOQA | 
|  | 15 | +import pycparser_monkeypatch # NOQA | 
|  | 16 | +import pycparser # NOQA | 
|  | 17 | + | 
|  | 18 | +temp_directory = tempfile.mkdtemp(suffix='.lvgl_json') | 
|  | 19 | + | 
|  | 20 | + | 
|  | 21 | +def run(lvgl_config_path, target_header, filter_private): | 
|  | 22 | + | 
|  | 23 | + pycparser_monkeypatch.FILTER_PRIVATE = filter_private | 
|  | 24 | + | 
|  | 25 | + lvgl_path = project_path | 
|  | 26 | + lvgl_src_path = os.path.join(lvgl_path, 'src') | 
|  | 27 | + temp_lvgl = os.path.join(temp_directory, 'lvgl') | 
|  | 28 | + target_header_base_name = ( | 
|  | 29 | + os.path.splitext(os.path.split(target_header)[-1])[0] | 
|  | 30 | + ) | 
|  | 31 | + | 
|  | 32 | + os.mkdir(temp_lvgl) | 
|  | 33 | + shutil.copytree(lvgl_src_path, os.path.join(temp_lvgl, 'src')) | 
|  | 34 | + shutil.copyfile(os.path.join(lvgl_path, 'lvgl.h'), os.path.join(temp_lvgl, 'lvgl.h')) | 
|  | 35 | + | 
|  | 36 | + pp_file = os.path.join(temp_directory, target_header_base_name + '.pp') | 
|  | 37 | + | 
|  | 38 | + src = lvgl_config_path | 
|  | 39 | + dst = os.path.join(temp_directory, 'lv_conf.h') | 
|  | 40 | + shutil.copyfile(src, dst) | 
|  | 41 | + | 
|  | 42 | + include_dirs = [temp_directory, project_path] | 
|  | 43 | + | 
|  | 44 | + if sys.platform.startswith('darwin'): | 
|  | 45 | + include_path_env_key = 'C_INCLUDE_PATH' | 
|  | 46 | + cpp_cmd = [ | 
|  | 47 | + 'clang', '-std=c11', '-E', '-DINT32_MIN=0x80000000', | 
|  | 48 | + ] | 
|  | 49 | + output_pp = f' >> "{pp_file}"' | 
|  | 50 | + else: | 
|  | 51 | + include_path_env_key = 'C_INCLUDE_PATH' | 
|  | 52 | + cpp_cmd = [ | 
|  | 53 | + 'gcc', '-std=c11', '-E', '-Wno-incompatible-pointer-types', | 
|  | 54 | + ] | 
|  | 55 | + output_pp = f' >> "{pp_file}"' | 
|  | 56 | + | 
|  | 57 | + fake_libc_path = create_fake_lib_c.run(temp_directory) | 
|  | 58 | + | 
|  | 59 | + if include_path_env_key not in os.environ: | 
|  | 60 | + os.environ[include_path_env_key] = '' | 
|  | 61 | + | 
|  | 62 | + os.environ[include_path_env_key] = ( | 
|  | 63 | + f'{fake_libc_path}{os.pathsep}{os.environ[include_path_env_key]}' | 
|  | 64 | + ) | 
|  | 65 | + | 
|  | 66 | + if 'PATH' not in os.environ: | 
|  | 67 | + os.environ['PATH'] = '' | 
|  | 68 | + | 
|  | 69 | + os.environ['PATH'] = ( | 
|  | 70 | + f'{fake_libc_path}{os.pathsep}{os.environ["PATH"]}' | 
|  | 71 | + ) | 
|  | 72 | + | 
|  | 73 | + cpp_cmd.extend([ | 
|  | 74 | + '-DLV_LVGL_H_INCLUDE_SIMPLE', | 
|  | 75 | + '-DLV_CONF_INCLUDE_SIMPLE', | 
|  | 76 | + '-DLV_USE_DEV_VERSION' | 
|  | 77 | + ]) | 
|  | 78 | + | 
|  | 79 | + cpp_cmd.extend(['-DPYCPARSER', f'"-I{fake_libc_path}"']) | 
|  | 80 | + cpp_cmd.extend([f'"-I{item}"' for item in include_dirs]) | 
|  | 81 | + cpp_cmd.append(f'"{target_header}"') | 
|  | 82 | + | 
|  | 83 | + if sys.platform.startswith('win'): | 
|  | 84 | + cpp_cmd.insert(len(cpp_cmd) - 2, output_pp) | 
|  | 85 | + else: | 
|  | 86 | + cpp_cmd.append(output_pp) | 
|  | 87 | + | 
|  | 88 | + cpp_cmd = ' '.join(cpp_cmd) | 
|  | 89 | + | 
|  | 90 | + p = subprocess.Popen( | 
|  | 91 | + cpp_cmd, | 
|  | 92 | + stdout=subprocess.PIPE, | 
|  | 93 | + stderr=subprocess.PIPE, | 
|  | 94 | + env=os.environ, | 
|  | 95 | + shell=True | 
|  | 96 | + ) | 
|  | 97 | + out, err = p.communicate() | 
|  | 98 | + exit_code = p.returncode | 
|  | 99 | + | 
|  | 100 | + if not os.path.exists(pp_file): | 
|  | 101 | + sys.stdout.write(out.decode('utf-8').strip() + '\n') | 
|  | 102 | + sys.stdout.write('EXIT CODE: ' + str(exit_code) + '\n') | 
|  | 103 | + sys.stderr.write(err.decode('utf-8').strip() + '\n') | 
|  | 104 | + sys.stdout.flush() | 
|  | 105 | + sys.stderr.flush() | 
|  | 106 | + | 
|  | 107 | + raise RuntimeError('Unknown Failure') | 
|  | 108 | + | 
|  | 109 | + with open(pp_file, 'r') as f: | 
|  | 110 | + pp_data = f.read() | 
|  | 111 | + | 
|  | 112 | + cparser = pycparser.CParser() | 
|  | 113 | + ast = cparser.parse(pp_data, target_header) | 
|  | 114 | + | 
|  | 115 | + ast.setup_docs(temp_directory) | 
|  | 116 | + | 
|  | 117 | + shutil.rmtree(temp_directory) | 
|  | 118 | + | 
|  | 119 | + return ast | 
|  | 120 | + | 
0 commit comments