# Copyright 2012-2017 The Meson development team # 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. import os.path import typing from .. import mlog from ..mesonlib import EnvironmentException, MachineChoice, Popen_safe from .compilers import (Compiler, cuda_buildtype_args, cuda_optimization_args, cuda_debug_args, CompilerType) if typing.TYPE_CHECKING: from ..environment import Environment # noqa: F401 class CudaCompiler(Compiler): def __init__(self, exelist, version, for_machine: MachineChoice, is_cross, exe_wrapper=None, **kwargs): if not hasattr(self, 'language'): self.language = 'cuda' super().__init__(exelist, version, for_machine, **kwargs) self.is_cross = is_cross self.exe_wrapper = exe_wrapper self.id = 'nvcc' default_warn_args = [] self.warn_args = {'0': [], '1': default_warn_args, '2': default_warn_args + ['-Xcompiler=-Wextra'], '3': default_warn_args + ['-Xcompiler=-Wextra', '-Xcompiler=-Wpedantic']} def needs_static_linker(self): return False def get_always_args(self): return [] def get_display_language(self): return 'Cuda' def get_no_stdinc_args(self): return [] def thread_link_flags(self, environment): return self._cook_link_args(super().thread_link_flags()) def sanity_check(self, work_dir, environment): mlog.debug('Sanity testing ' + self.get_display_language() + ' compiler:', ' '.join(self.exelist)) mlog.debug('Is cross compiler: %s.' % str(self.is_cross)) sname = 'sanitycheckcuda.cu' code = r''' #include #include __global__ void kernel (void) {} int main(void){ struct cudaDeviceProp prop; int count, i; cudaError_t ret = cudaGetDeviceCount(&count); if(ret != cudaSuccess){ fprintf(stderr, "%d\n", (int)ret); }else{ for(i=0;i using {symbol}; int main () {{ return 0; }}''' return self.compiles(t.format(**fargs), env, extra_args, dependencies) @staticmethod def _cook_link_args(args: typing.List[str]) -> typing.List[str]: """ Converts GNU-style arguments -Wl,-arg,-arg to NVCC-style arguments -Xlinker=-arg,-arg """ cooked = [] # type: typing.List[str] for arg in args: if arg.startswith('-Wl,'): arg = arg.replace('-Wl,', '-Xlinker=', 1) arg = arg.replace(' ', '\\') cooked.append(arg) return cooked def name_string(self): return ' '.join(self.exelist) def get_soname_args(self, *args): return self._cook_link_args(super().get_soname_args(*args)) def get_dependency_gen_args(self, outtarget, outfile): return [] def get_compile_only_args(self): return ['-c'] def get_no_optimization_args(self): return ['-O0'] def get_optimization_args(self, optimization_level): return cuda_optimization_args[optimization_level] def get_debug_args(self, is_debug): return cuda_debug_args[is_debug] def get_werror_args(self): return ['-Werror=cross-execution-space-call,deprecated-declarations,reorder'] def get_warn_args(self, level): return self.warn_args[level] def get_buildtype_args(self, buildtype): return cuda_buildtype_args[buildtype] def get_include_args(self, path, is_system): if path == '': path = '.' return ['-I' + path] def depfile_for_object(self, objfile): return objfile + '.' + self.get_depfile_suffix() def get_depfile_suffix(self): return 'd' def build_rpath_args(self, env: 'Environment', build_dir: str, from_dir: str, rpath_paths: str, build_rpath: str, install_rpath: str) -> typing.List[str]: return self._cook_link_args(super().build_rpath_args( env, build_dir, from_dir, rpath_paths, build_rpath, install_rpath)) def linker_to_compiler_args(self, args): return args def get_pic_args(self): return ['-Xcompiler=-fPIC'] def compute_parameters_with_absolute_paths(self, parameter_list, build_dir): return [] def get_output_args(self, target: str) -> typing.List[str]: return ['-o', target] def get_std_exe_link_args(self) -> typing.List[str]: return []