forked from intelxed/xed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmfile.py
executable file
·146 lines (126 loc) · 4.42 KB
/
mfile.py
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
#!/usr/bin/env python3
# -*- python -*-
#BEGIN_LEGAL
#
#Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
import sys
import os
from platform import system
from setuptools import setup, Extension
# Assume mbuild is next to the current source directory
# put mbuild on the import path
# from "path-to-xed/xed2/mfile.py" obtain: "path-to-xed/xed2".
def find_dir(d):
dir = os.getcwd()
last = ''
while dir != last:
target_dir = os.path.join(dir,d)
if os.path.exists(target_dir):
return target_dir
last = dir
(dir,tail) = os.path.split(dir)
return None
def fatal(m):
sys.stderr.write("\n\nXED build error: %s\n\n" % (m) )
sys.exit(1)
def try_mbuild_import():
try:
import mbuild
return True
except:
return False
def find_mbuild_import():
if try_mbuild_import():
# mbuild is already findable by PYTHONPATH. Nothing required from
# this function.
return
script_name = sys.argv[0]
mbuild_install_path_derived = \
os.path.join(os.path.dirname(script_name), '..', 'mbuild')
mbuild_install_path_relative = find_dir('mbuild')
mbuild_install_path = mbuild_install_path_derived
if not os.path.exists(mbuild_install_path):
if not mbuild_install_path_relative:
# If find_dir() fails, it returns None. That messes
# up os.path.exists() so we fix it with ''
mbuild_install_path_relative=''
mbuild_install_path = mbuild_install_path_relative
if not os.path.exists(mbuild_install_path):
s = "mfile.py cannot find the mbuild directory: [%s] or [%s]"
fatal(s % (mbuild_install_path_derived,
mbuild_install_path_relative))
# modify the environment python path so that the imported modules
# (enumer,codegen) can find mbuild.
if 'PYTHONPATH' in os.environ:
sep = os.pathsep
os.environ['PYTHONPATH'] = mbuild_install_path + sep + \
os.environ['PYTHONPATH']
else:
os.environ['PYTHONPATH'] = mbuild_install_path
sys.path.insert(0,mbuild_install_path)
def work():
if sys.version_info[0] == 3:
if sys.version_info[1] < 4:
fatal("Need python version 3.4 or later.")
else:
fatal("Need python version 3.4 or later.")
try:
find_mbuild_import()
except:
fatal("mbuild import failed")
import xed_mbuild
import xed_build_common
try:
retval = xed_mbuild.execute()
except Exception as e:
xed_build_common.handle_exception_and_die(e)
return retval
def buildPyModule():
setup(name='xed',
version='1.0',
options={'build':{'build_lib':'.'}},
script_args=['build'],
ext_modules=[
Extension('xed',
include_dirs = ['obj/wkit/include'],
library_dirs = ['obj/wkit/lib'],
libraries = ['xed'],
define_macros = [('PYTHON', None)],
sources = ['examples/xed-examples-util.c',
'examples/xed-nm-symtab.c',
'examples/xed-disas-raw.c',
'examples/xed-dot-prep.c',
'examples/xed-symbol-table.c',
'examples/xed-dot.c',
'examples/avltree.c',
'examples/xed-disas-elf.c',
'examples/xed-disas-macho.c'] +
(['examples/xed-disas-pecoff.cpp'] if system() == 'Windows' else [])
)
]
)
if __name__ == "__main__":
buildpm = False
if 'pymodule' in sys.argv:
buildpm = True
sys.argv.remove('pymodule')
sys.argv.append('--extra-flags=-fPIC')
retval = work()
if retval:
sys.exit(retval)
if buildpm:
buildPyModule()