88import csv
99import logging
1010import pathlib
11+ import platform
12+ import sys
13+ import tarfile
1114
12- from .build import build_arch , extract_archive , get_triplet
15+ __version__ = "0.1.8"
16+
17+ log = logging .getLogger (__name__ )
1318
14- __version__ = "0.1.7"
1519
16- triplet = get_triplet (build_arch ())
20+ class PPBTException (Exception ):
21+ """
22+ Base class for all ppbt exceptions.
1723
18- archive = pathlib .Path (__file__ ).parent / "_toolchain" / f"{ triplet } .tar.xz"
19- toolchain = pathlib .Path (__file__ ).parent / "_toolchain" / triplet
20- toolchain_root = pathlib .Path (__file__ ).parent / "_toolchain"
24+ """
25+
26+
27+ def get_triplet (machine = None , plat = None ):
28+ """
29+ Get the target triplet for the specified machine and platform.
30+
31+ If any of the args are None, it will try to deduce what they should be.
32+
33+ :param machine: The machine for the triplet
34+ :type machine: str
35+ :param plat: The platform for the triplet
36+ :type plat: str
37+
38+ :raises BuildError: If the platform is unknown
39+
40+ :return: The target triplet
41+ :rtype: str
42+ """
43+ if not plat :
44+ plat = sys .platform
45+ if not machine :
46+ machine = build_arch ()
47+ if plat == "darwin" :
48+ return f"{ machine } -macos"
49+ elif plat == "win32" :
50+ return f"{ machine } -win"
51+ elif plat == "linux" :
52+ return f"{ machine } -linux-gnu"
53+ else :
54+ raise PPBTException (f"Unknown platform { plat } " )
55+
56+
57+ def build_arch ():
58+ """
59+ Return the current machine.
60+ """
61+ machine = platform .machine ()
62+ return machine .lower ()
63+
64+
65+ TRIPLET = get_triplet (build_arch ())
66+ ARCHIVE = pathlib .Path (__file__ ).parent / "_toolchain" / f"{ TRIPLET } .tar.xz"
67+ TOOLCHAIN_ROOT = pathlib .Path (__file__ ).parent / "_toolchain"
68+ TOOLCHAIN = TOOLCHAIN_ROOT / TRIPLET
2169
2270# This is not reliable, the version can be modified by setuptools at build time.
23- distinfo = (
71+ DISTINFO = (
2472 pathlib .Path (__file__ ).resolve ().parent .parent / f"ppbt-{ __version__ } .dist-info"
2573)
2674
27- log = logging .getLogger (__name__ )
75+
76+ def extract_archive (to_dir , archive ):
77+ """
78+ Extract an archive to a specific location.
79+
80+ :param to_dir: The directory to extract to
81+ :type to_dir: str
82+ :param archive: The archive to extract
83+ :type archive: str
84+ """
85+ if archive .endswith ("tgz" ):
86+ read_type = "r:gz"
87+ elif archive .endswith ("xz" ):
88+ read_type = "r:xz"
89+ elif archive .endswith ("bz2" ):
90+ read_type = "r:bz2"
91+ else :
92+ read_type = "r"
93+ with tarfile .open (archive , read_type ) as t :
94+ t .extractall (to_dir )
2895
2996
3097def extract (overwrite = False ):
3198 """
3299 Extract the toolchain tarball.
33100 """
34- if toolchain .exists () and not overwrite :
101+ if TOOLCHAIN .exists () and not overwrite :
35102 log .debug ("Toolchain directory exists" )
36103 else :
37104 log .info ("Extract archive" )
38- extract_archive (toolchain_root , str (archive ))
39- record = distinfo / "RECORD"
105+ extract_archive (TOOLCHAIN_ROOT , str (ARCHIVE ))
106+ record = DISTINFO / "RECORD"
40107 if record .exists ():
41108 records = []
42109 log .info ("Update pkg metadata" )
43110 with open (record , "r" ) as fp :
44111 for row in csv .reader (fp ):
45112 records .append (row )
46- with open (str (archive ) + ".record" , "r" ) as fp :
113+ with open (str (ARCHIVE ) + ".record" , "r" ) as fp :
47114 for row in csv .reader (fp ):
48115 records .append (row )
49116 records = sorted (records , key = lambda _ : _ [0 ])
@@ -57,18 +124,18 @@ def environ(auto_extract=False):
57124 """
58125 Toolchain build environment.
59126 """
60- if not toolchain .exists ():
127+ if not TOOLCHAIN .exists ():
61128 if auto_extract :
62129 extract ()
63130 else :
64131 raise RuntimeError ("Toolchain not extracted" )
65- basebin = toolchain / "bin" / triplet
132+ basebin = TOOLCHAIN / "bin" / TRIPLET
66133 return {
67- "TOOLCHAIN_PATH" : f"{ toolchain } " ,
134+ "TOOLCHAIN_PATH" : f"{ TOOLCHAIN } " ,
68135 "CC" : f"{ basebin } -gcc" ,
69136 "CXX" : f"{ basebin } -g++" ,
70- "CFLAGS" : f"-I{ toolchain } /{ triplet } /sysroot/usr/include" ,
71- "CPPFLAGS" : f"-I{ toolchain } /{ triplet } /sysroot/usr/include" ,
72- "CMAKE_FLAGS" : f"-I{ toolchain } /{ triplet } /sysroot/usr/include" ,
73- "LDFLAGS" : f"-L{ toolchain } /{ triplet } /sysroot/lib" ,
137+ "CFLAGS" : f"-I{ TOOLCHAIN } /{ TRIPLET } /sysroot/usr/include" ,
138+ "CPPFLAGS" : f"-I{ TOOLCHAIN } /{ TRIPLET } /sysroot/usr/include" ,
139+ "CMAKE_FLAGS" : f"-I{ TOOLCHAIN } /{ TRIPLET } /sysroot/usr/include" ,
140+ "LDFLAGS" : f"-L{ TOOLCHAIN } /{ TRIPLET } /sysroot/lib" ,
74141 }
0 commit comments