From 54b6afa675a248ca46897acf47f9bf8cf06f047d Mon Sep 17 00:00:00 2001 From: Beau Johnston Date: Sun, 10 Jun 2018 07:42:50 +1000 Subject: added cuda compiler --- mesonbuild/compilers/__init__.py | 2 + mesonbuild/compilers/compilers.py | 11 ++- mesonbuild/compilers/cuda.py | 188 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 mesonbuild/compilers/cuda.py (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/__init__.py b/mesonbuild/compilers/__init__.py index c568a98..60cca93 100644 --- a/mesonbuild/compilers/__init__.py +++ b/mesonbuild/compilers/__init__.py @@ -72,6 +72,7 @@ __all__ = [ 'JavaCompiler', 'LLVMDCompiler', 'MonoCompiler', + 'NvidiaCudaCompiler', 'VisualStudioCsCompiler', 'NAGFortranCompiler', 'ObjCCompiler', @@ -153,6 +154,7 @@ from .d import ( GnuDCompiler, LLVMDCompiler, ) +from .cuda import CudaCompiler from .fortran import ( FortranCompiler, G95FortranCompiler, diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 016e704..b2dc213 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -37,6 +37,7 @@ lib_suffixes = ('a', 'lib', 'dll', 'dylib', 'so') lang_suffixes = { 'c': ('c',), 'cpp': ('cpp', 'cc', 'cxx', 'c++', 'hh', 'hpp', 'ipp', 'hxx'), + 'cuda': ('cu',), # f90, f95, f03, f08 are for free-form fortran ('f90' recommended) # f, for, ftn, fpp are for fixed-form fortran ('f' or 'for' recommended) 'fortran': ('f90', 'f95', 'f03', 'f08', 'f', 'for', 'ftn', 'fpp'), @@ -58,7 +59,7 @@ clib_langs = ('objcpp', 'cpp', 'objc', 'c', 'fortran',) # List of languages that can be linked with C code directly by the linker # used in build.py:process_compilers() and build.py:get_dynamic_linker() # XXX: Add Rust to this? -clink_langs = ('d',) + clib_langs +clink_langs = ('d', 'cuda') + clib_langs clink_suffixes = () for _l in clink_langs + ('vala',): clink_suffixes += lang_suffixes[_l] @@ -69,6 +70,7 @@ soregex = re.compile(r'.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$') # Environment variables that each lang uses. cflags_mapping = {'c': 'CFLAGS', 'cpp': 'CXXFLAGS', + 'cuda': 'CUFLAGS', 'objc': 'OBJCFLAGS', 'objcpp': 'OBJCXXFLAGS', 'fortran': 'FFLAGS', @@ -143,6 +145,13 @@ armclang_buildtype_args = {'plain': [], 'custom': [], } +cuda_buildtype_args = {'plain': [], + 'debug': ['-O0', '-g'], + 'debugoptimized': ['-O1', '--debug'], + 'release': ['-O3', '-Otime'], + 'minsize': ['-O3', '-Ospace'], + } + arm_buildtype_args = {'plain': [], 'debug': ['-O0', '--debug'], 'debugoptimized': ['-O1', '--debug'], diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py new file mode 100644 index 0000000..f398bad --- /dev/null +++ b/mesonbuild/compilers/cuda.py @@ -0,0 +1,188 @@ +# 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 subprocess, os.path + +from .. import mlog +from ..mesonlib import EnvironmentException, Popen_safe +from .compilers import Compiler, cuda_buildtype_args + +class CudaCompiler(Compiler): + def __init__(self, exelist, version, is_cross, exe_wrapper=None): + # If a child ObjCPP class has already set it, don't set it ourselves + if not hasattr(self, 'language'): + self.language = 'cuda' + super().__init__(exelist, version) + self.is_cross = is_cross + self.exe_wrapper = exe_wrapper + self.id = 'nvcc' + default_warn_args = [] + self.warn_args = {'1': default_warn_args, + '2': default_warn_args + ['-Wextra'], + '3': default_warn_args + ['-Wextra', '-Wpedantic']} + + def needs_static_linker(self): + return False + + def get_display_language(self): + return 'Cuda' + + def get_no_stdinc_args(self): + return [''] + + def sanity_check(self, work_dir, environment): + source_name = os.path.join(work_dir, 'sanitycheckcuda.cu') + binary_name = os.path.join(work_dir, 'sanitycheckcuda') + extra_flags = self.get_cross_extra_flags(environment, link=False) + if self.is_cross: + extra_flags += self.get_compile_only_args() + + code = ''' + #include + int main(int argc,char** argv){ + return 0; + } + ''' + + with open(source_name, 'w') as ofile: + ofile.write(code) + pc = subprocess.Popen(self.exelist + extra_flags + [source_name, '-o', binary_name]) + pc.wait() + if pc.returncode != 0: + raise EnvironmentException('Cuda compiler %s can not compile programs.' % self.name_string()) + if self.is_cross: + # Can't check if the binaries run so we have to assume they do + return + pe = subprocess.Popen(binary_name) + pe.wait() + if pe.returncode != 0: + raise EnvironmentException('Executables created by ObjC compiler %s are not runnable.' % self.name_string()) + + def get_compiler_check_args(self): + return super().get_compiler_check_args() + [] + + def has_header_symbol(self, hname, symbol, prefix, env, extra_args=None, dependencies=None): + if super().has_header_symbol(hname, symbol, prefix, env, extra_args, dependencies): + return True + if extra_args is None: + extra_args = [] + fargs = {'prefix': prefix, 'header': hname, 'symbol': symbol} + t = '''{prefix} + #include <{header}> + using {symbol}; + int main () {{ return 0; }}''' + return self.compiles(t.format(**fargs), env, extra_args, dependencies) + + def sanity_check_impl(self, work_dir, environment, sname, code): + mlog.debug('Sanity testing ' + self.get_display_language() + ' compiler:', ' '.join(self.exelist)) + mlog.debug('Is cross compiler: %s.' % str(self.is_cross)) + + extra_flags = [] + source_name = os.path.join(work_dir, sname) + binname = sname.rsplit('.', 1)[0] + if self.is_cross: + binname += '_cross' + if self.exe_wrapper is None: + # Linking cross built apps is painful. You can't really + # tell if you should use -nostdlib or not and for example + # on OSX the compiler binary is the same but you need + # a ton of compiler flags to differentiate between + # arm and x86_64. So just compile. + extra_flags += self.get_cross_extra_flags(environment, link=False) + extra_flags += self.get_compile_only_args() + else: + extra_flags += self.get_cross_extra_flags(environment, link=True) + # Is a valid executable output for all toolchains and platforms + binname += '.exe' + # Write binary check source + binary_name = os.path.join(work_dir, binname) + with open(source_name, 'w') as ofile: + ofile.write(code) + # Compile sanity check + cmdlist = self.exelist + extra_flags + [source_name] + self.get_output_args(binary_name) + pc, stdo, stde = Popen_safe(cmdlist, cwd=work_dir) + mlog.debug('Sanity check compiler command line:', ' '.join(cmdlist)) + mlog.debug('Sanity check compile stdout:') + mlog.debug(stdo) + mlog.debug('-----\nSanity check compile stderr:') + mlog.debug(stde) + mlog.debug('-----') + if pc.returncode != 0: + raise EnvironmentException('Compiler {0} can not compile programs.'.format(self.name_string())) + # Run sanity check + if self.is_cross: + if self.exe_wrapper is None: + # Can't check if the binaries run so we have to assume they do + return + cmdlist = self.exe_wrapper + [binary_name] + else: + cmdlist = [binary_name] + mlog.debug('Running test binary command: ' + ' '.join(cmdlist)) + pe = subprocess.Popen(cmdlist) + pe.wait() + if pe.returncode != 0: + raise EnvironmentException('Executables created by {0} compiler {1} are not runnable.'.format(self.language, self.name_string())) + + def get_output_args(self, target): + return ['-o', target] + + def name_string(self): + return ' '.join(self.exelist) + + 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_linker_exelist(self): + return self.exelist[:] + + def get_linker_output_args(self, outputname): + return ['-o', outputname] + + 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 get_buildtype_linker_args(self, buildtype): + return [] + + def get_std_exe_link_args(self): + return [] + + def build_rpath_args(self, build_dir, from_dir, rpath_paths, build_rpath, install_rpath): + return [] + + def get_linker_search_args(self, dirname): + return ['/LIBPATH:' + dirname] + + def linker_to_compiler_args(self, args): + return ['/link'] + args -- cgit v1.1 From 396e355c940d1427ccd7be7435499074f56bab7e Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Fri, 20 Jul 2018 18:45:36 +0300 Subject: Clean up minor issues. --- mesonbuild/compilers/cuda.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index f398bad..a8cd7bc 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -20,7 +20,6 @@ from .compilers import Compiler, cuda_buildtype_args class CudaCompiler(Compiler): def __init__(self, exelist, version, is_cross, exe_wrapper=None): - # If a child ObjCPP class has already set it, don't set it ourselves if not hasattr(self, 'language'): self.language = 'cuda' super().__init__(exelist, version) @@ -39,7 +38,7 @@ class CudaCompiler(Compiler): return 'Cuda' def get_no_stdinc_args(self): - return [''] + return [] def sanity_check(self, work_dir, environment): source_name = os.path.join(work_dir, 'sanitycheckcuda.cu') @@ -49,7 +48,10 @@ class CudaCompiler(Compiler): extra_flags += self.get_compile_only_args() code = ''' - #include +__global__ void kernel (void) { + +} + int main(int argc,char** argv){ return 0; } @@ -67,7 +69,7 @@ class CudaCompiler(Compiler): pe = subprocess.Popen(binary_name) pe.wait() if pe.returncode != 0: - raise EnvironmentException('Executables created by ObjC compiler %s are not runnable.' % self.name_string()) + raise EnvironmentException('Executables created by Cuda compiler %s are not runnable.' % self.name_string()) def get_compiler_check_args(self): return super().get_compiler_check_args() + [] -- cgit v1.1 From 8eca221aac333489004953c4149011b5d74df176 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Fri, 20 Jul 2018 19:26:42 +0300 Subject: More tests and pic. --- mesonbuild/compilers/cuda.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index a8cd7bc..7beecf9 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -188,3 +188,6 @@ __global__ void kernel (void) { def linker_to_compiler_args(self, args): return ['/link'] + args + + def get_pic_args(self): + return [] -- cgit v1.1 From e1b50309dfd9c927866f36a0a4662321351ab697 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Mon, 21 Jan 2019 23:59:20 +0200 Subject: All the fixes needed to make work against current master. --- mesonbuild/compilers/compilers.py | 19 +++++++++++++++---- mesonbuild/compilers/cuda.py | 11 ++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index b2dc213..b1f3cc2 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -146,10 +146,10 @@ armclang_buildtype_args = {'plain': [], } cuda_buildtype_args = {'plain': [], - 'debug': ['-O0', '-g'], - 'debugoptimized': ['-O1', '--debug'], - 'release': ['-O3', '-Otime'], - 'minsize': ['-O3', '-Ospace'], + 'debug': [], + 'debugoptimized': [], + 'release': [], + 'minsize': [], } arm_buildtype_args = {'plain': [], @@ -354,6 +354,17 @@ msvc_optimization_args = {'0': [], 's': ['/O1'], # Implies /Os. } +cuda_optimization_args = {'0': [], + 'g': ['-O0'], + '1': ['-O1'], + '2': ['-O2'], + '3': ['-O3', '-Otime'], + 's': ['-O3', '-Ospace'] + } + +cuda_debug_args = {False: [], + True: ['-g']} + clike_debug_args = {False: [], True: ['-g']} diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index 7beecf9..b5f16ab 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -16,7 +16,7 @@ import subprocess, os.path from .. import mlog from ..mesonlib import EnvironmentException, Popen_safe -from .compilers import Compiler, cuda_buildtype_args +from .compilers import Compiler, cuda_buildtype_args, cuda_optimization_args, cuda_debug_args class CudaCompiler(Compiler): def __init__(self, exelist, version, is_cross, exe_wrapper=None): @@ -151,6 +151,12 @@ __global__ void kernel (void) { 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_linker_exelist(self): return self.exelist[:] @@ -191,3 +197,6 @@ __global__ void kernel (void) { def get_pic_args(self): return [] + + def compute_parameters_with_absolute_paths(self, parameter_list, build_dir): + return [] -- cgit v1.1