|
| 1 | +"""distutils.zosccompiler |
| 2 | +
|
| 3 | +Contains the selection of the c & c++ compilers on z/OS. There are several |
| 4 | +different c compilers on z/OS, all of them are optional, so the correct |
| 5 | +one needs to be chosen based on the users input. This is compatible with |
| 6 | +the following compilers: |
| 7 | +
|
| 8 | +IBM C/C++ For Open Enterprise Languages on z/OS 2.0 |
| 9 | +IBM Open XL C/C++ 1.1 for z/OS |
| 10 | +IBM XL C/C++ V2.4.1 for z/OS 2.4 and 2.5 |
| 11 | +IBM z/OS XL C/C++ |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +from .unixccompiler import UnixCCompiler |
| 16 | +from . import sysconfig |
| 17 | +from .errors import DistutilsExecError, CompileError |
| 18 | + |
| 19 | +_cc_args = { |
| 20 | + 'ibm-openxl': [ |
| 21 | + '-m64', |
| 22 | + '-fvisibility=default', |
| 23 | + '-fzos-le-char-mode=ascii', |
| 24 | + '-fno-short-enums', |
| 25 | + ], |
| 26 | + 'ibm-xlclang': [ |
| 27 | + '-q64', |
| 28 | + '-qexportall', |
| 29 | + '-qascii', |
| 30 | + '-qstrict', |
| 31 | + '-qnocsect', |
| 32 | + '-Wa,asa,goff', |
| 33 | + '-Wa,xplink', |
| 34 | + '-qgonumber', |
| 35 | + '-qenum=int', |
| 36 | + '-Wc,DLL', |
| 37 | + ], |
| 38 | + 'ibm-xlc': [ |
| 39 | + '-q64', |
| 40 | + '-qexportall', |
| 41 | + '-qascii', |
| 42 | + '-qstrict', |
| 43 | + '-qnocsect', |
| 44 | + '-Wa,asa,goff', |
| 45 | + '-Wa,xplink', |
| 46 | + '-qgonumber', |
| 47 | + '-qenum=int', |
| 48 | + '-Wc,DLL', |
| 49 | + '-qlanglvl=extc99', |
| 50 | + ], |
| 51 | +} |
| 52 | + |
| 53 | +_cxx_args = { |
| 54 | + 'ibm-openxl': [ |
| 55 | + '-m64', |
| 56 | + '-fvisibility=default', |
| 57 | + '-fzos-le-char-mode=ascii', |
| 58 | + '-fno-short-enums', |
| 59 | + ], |
| 60 | + 'ibm-xlclang': [ |
| 61 | + '-q64', |
| 62 | + '-qexportall', |
| 63 | + '-qascii', |
| 64 | + '-qstrict', |
| 65 | + '-qnocsect', |
| 66 | + '-Wa,asa,goff', |
| 67 | + '-Wa,xplink', |
| 68 | + '-qgonumber', |
| 69 | + '-qenum=int', |
| 70 | + '-Wc,DLL', |
| 71 | + ], |
| 72 | + 'ibm-xlc': [ |
| 73 | + '-q64', |
| 74 | + '-qexportall', |
| 75 | + '-qascii', |
| 76 | + '-qstrict', |
| 77 | + '-qnocsect', |
| 78 | + '-Wa,asa,goff', |
| 79 | + '-Wa,xplink', |
| 80 | + '-qgonumber', |
| 81 | + '-qenum=int', |
| 82 | + '-Wc,DLL', |
| 83 | + '-qlanglvl=extended0x', |
| 84 | + ], |
| 85 | +} |
| 86 | + |
| 87 | +_asm_args = { |
| 88 | + 'ibm-openxl': ['-fasm', '-fno-integrated-as', '-Wa,--ASA', '-Wa,--GOFF'], |
| 89 | + 'ibm-xlclang': [], |
| 90 | + 'ibm-xlc': [], |
| 91 | +} |
| 92 | + |
| 93 | +_ld_args = { |
| 94 | + 'ibm-openxl': [], |
| 95 | + 'ibm-xlclang': ['-Wl,dll', '-q64'], |
| 96 | + 'ibm-xlc': ['-Wl,dll', '-q64'], |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +# Python on z/OS is built with no compiler specific options in it's CFLAGS. |
| 101 | +# But each compiler requires it's own specific options to build successfully, |
| 102 | +# though some of the options are common between them |
| 103 | +class zOSCCompiler(UnixCCompiler): |
| 104 | + src_extensions = ['.c', '.C', '.cc', '.cxx', '.cpp', '.m', '.s'] |
| 105 | + _cpp_extensions = ['.cc', '.cpp', '.cxx', '.C'] |
| 106 | + _asm_extensions = ['.s'] |
| 107 | + |
| 108 | + def _get_zos_compiler_name(self): |
| 109 | + zos_compiler_names = [ |
| 110 | + os.path.basename(binary) |
| 111 | + for envvar in ('CC', 'CXX', 'LDSHARED') |
| 112 | + if (binary := os.environ.get(envvar, None)) |
| 113 | + ] |
| 114 | + if len(zos_compiler_names) == 0: |
| 115 | + return 'ibm-openxl' |
| 116 | + |
| 117 | + zos_compilers = {} |
| 118 | + for compiler in ( |
| 119 | + 'ibm-clang', |
| 120 | + 'ibm-clang64', |
| 121 | + 'ibm-clang++', |
| 122 | + 'ibm-clang++64', |
| 123 | + 'clang', |
| 124 | + 'clang++', |
| 125 | + 'clang-14', |
| 126 | + ): |
| 127 | + zos_compilers[compiler] = 'ibm-openxl' |
| 128 | + |
| 129 | + for compiler in ('xlclang', 'xlclang++', 'njsc', 'njsc++'): |
| 130 | + zos_compilers[compiler] = 'ibm-xlclang' |
| 131 | + |
| 132 | + for compiler in ('xlc', 'xlC', 'xlc++'): |
| 133 | + zos_compilers[compiler] = 'ibm-xlc' |
| 134 | + |
| 135 | + return zos_compilers.get(zos_compiler_names[0], 'ibm-openxl') |
| 136 | + |
| 137 | + def __init__(self, verbose=0, dry_run=0, force=0): |
| 138 | + super().__init__(verbose, dry_run, force) |
| 139 | + self.zos_compiler = self._get_zos_compiler_name() |
| 140 | + sysconfig.customize_compiler(self) |
| 141 | + |
| 142 | + def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 143 | + local_args = [] |
| 144 | + if ext in self._cpp_extensions: |
| 145 | + compiler = self.compiler_cxx |
| 146 | + local_args.extend(_cxx_args[self.zos_compiler]) |
| 147 | + elif ext in self._asm_extensions: |
| 148 | + compiler = self.compiler_so |
| 149 | + local_args.extend(_cc_args[self.zos_compiler]) |
| 150 | + local_args.extend(_asm_args[self.zos_compiler]) |
| 151 | + else: |
| 152 | + compiler = self.compiler_so |
| 153 | + local_args.extend(_cc_args[self.zos_compiler]) |
| 154 | + local_args.extend(cc_args) |
| 155 | + |
| 156 | + try: |
| 157 | + self.spawn(compiler + local_args + [src, '-o', obj] + extra_postargs) |
| 158 | + except DistutilsExecError as msg: |
| 159 | + raise CompileError(msg) |
| 160 | + |
| 161 | + def runtime_library_dir_option(self, dir): |
| 162 | + return '-L' + dir |
| 163 | + |
| 164 | + def link( |
| 165 | + self, |
| 166 | + target_desc, |
| 167 | + objects, |
| 168 | + output_filename, |
| 169 | + output_dir=None, |
| 170 | + libraries=None, |
| 171 | + library_dirs=None, |
| 172 | + runtime_library_dirs=None, |
| 173 | + export_symbols=None, |
| 174 | + debug=0, |
| 175 | + extra_preargs=None, |
| 176 | + extra_postargs=None, |
| 177 | + build_temp=None, |
| 178 | + target_lang=None, |
| 179 | + ): |
| 180 | + # For a built module to use functions from cpython, it needs to use Pythons |
| 181 | + # side deck file. The side deck is located beside the libpython3.xx.so |
| 182 | + ldversion = sysconfig.get_config_var('LDVERSION') |
| 183 | + if sysconfig.python_build: |
| 184 | + side_deck_path = os.path.join( |
| 185 | + sysconfig.get_config_var('abs_builddir'), |
| 186 | + f'libpython{ldversion}.x', |
| 187 | + ) |
| 188 | + else: |
| 189 | + side_deck_path = os.path.join( |
| 190 | + sysconfig.get_config_var('installed_base'), |
| 191 | + sysconfig.get_config_var('platlibdir'), |
| 192 | + f'libpython{ldversion}.x', |
| 193 | + ) |
| 194 | + |
| 195 | + if os.path.exists(side_deck_path): |
| 196 | + if extra_postargs: |
| 197 | + extra_postargs.append(side_deck_path) |
| 198 | + else: |
| 199 | + extra_postargs = [side_deck_path] |
| 200 | + |
| 201 | + # Check and replace libraries included side deck files |
| 202 | + if runtime_library_dirs: |
| 203 | + for dir in runtime_library_dirs: |
| 204 | + for library in libraries[:]: |
| 205 | + library_side_deck = os.path.join(dir, f'{library}.x') |
| 206 | + if os.path.exists(library_side_deck): |
| 207 | + libraries.remove(library) |
| 208 | + extra_postargs.append(library_side_deck) |
| 209 | + break |
| 210 | + |
| 211 | + # Any required ld args for the given compiler |
| 212 | + extra_postargs.extend(_ld_args[self.zos_compiler]) |
| 213 | + |
| 214 | + super().link( |
| 215 | + target_desc, |
| 216 | + objects, |
| 217 | + output_filename, |
| 218 | + output_dir, |
| 219 | + libraries, |
| 220 | + library_dirs, |
| 221 | + runtime_library_dirs, |
| 222 | + export_symbols, |
| 223 | + debug, |
| 224 | + extra_preargs, |
| 225 | + extra_postargs, |
| 226 | + build_temp, |
| 227 | + target_lang, |
| 228 | + ) |
0 commit comments