How to determine which compiler was requested

2024/10/18 13:15:03

My project uses SCons to manage the build process. I want to support multiple compilers, so I decided to use AddOption so the user can specify which compiler to use on the command line (with the default being whatever their current compiler is).

AddOption('--compiler', dest = 'compiler', type = 'string', action = 'store', default = DefaultEnvironment()['CXX'], help = 'Name of the compiler to use.')

I want to be able to have built-in compiler settings for various compilers (including things such as maximum warning levels for that particular compiler). This is what my first attempt at a solution currently looks like:

if is_compiler('g++'):from build_scripts.gcc.std import cxx_stdfrom build_scripts.gcc.warnings import warnings, warnings_debug, warnings_optimizedfrom build_scripts.gcc.optimizations import optimizations, preprocessor_optimizations, linker_optimizations
elif is_compiler('clang++'):from build_scripts.clang.std import cxx_stdfrom build_scripts.clang.warnings import warnings, warnings_debug, warnings_optimizedfrom build_scripts.clang.optimizations import optimizations, preprocessor_optimizations, linker_optimizations

However, I'm not sure what to make the is_compiler() function look like. My first thought was to directly compare the compiler name (such as 'clang++') against what the user passes in. However, this immediately failed when I tried to use scons --compiler=~/data/llvm-3.1-obj/Release+Asserts/bin/clang++.

So I thought I'd get a little smarter and use this function

cxx = GetOption('compiler')
def is_compiler (compiler):return cxx[-len(compiler):] == compiler

This only looks at the end of the compiler string, so that it ignores directories. Unfortunately, 'clang++' ends in 'g++', so my compiler was seen to be g++ instead of clang++.

My next thought was to do a backward search and look for the first occurrence of a path separator ('\' or '/'), but then I realized that this won't work for people who have multiple compiler versions. Someone compiling with 'g++-4.7' will not register as being g++.

So, is there some simple way to determine which compiler was requested?

Currently, only g++ and clang++ are supported (and only their most recently released versions) due to their c++11 support, so a solution that only works for those two would be good enough for now. However, my ultimate goal is to support at least g++, clang++, icc, and msvc++ (once they support the required c++11 features), so more general solutions are preferred.

Answer

Compiler just are part of build process. Also you need linker tool and may be other additional programs. In Scons it's named - Tool. List of tools supported from box you can see in man page, search by statement: SCons supports the following tool specifications out of the box: ... Tool set necessary scons environment variables, it's documented here.

Scons automatically detects compiler in OS and have some priority to choose one of them, of course autodetect will work properly if PATH variable set to necessary dirs. For example of you have msvc and mingw on windows, scons choose msvc tool. For force using tool use Tool('name')(env). For example:

env = Environment()
Tool('mingw')(env)

Now env force using mingw.

So, clang is one of tool which currently not supported from box by scons. You need to implement it, or set env vars such CC, CXX which using scons for generate build commands.

https://en.xdnf.cn/q/73209.html

Related Q&A

Does pytest have anything like google tests non-fatal EXPECT_* behavior?

Im more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.From the documentation:Th…

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…

Librosa raised OSError(sndfile library not found) in Docker

Im trying to write the Dockerfile for a small python web project and there is something wrong with the dependencies. Ive been doing some search on the internet and it said that Librosa library requires…

Implementing Tags using Django rest framework

TDLR : what is the best way to implement tags in django-rest-framework. where the tags has a created_by field which is the currently authenticated user.I am trying to achieve a very simple/common thing…

Python audiolab install, unable to install (or find) libsndfile on Mac OSX

Trying to install scikits.audiolab-0.11.0 on Mac, bit it requires libsndfile: http://www.mega-nerd.com/libsndfile/. I did install libsndfile supposedly, using libsndfile_python-1.0.0-py2.7-macosx10.5.m…

Connecting to events of another widget

This is most likely a duplicate question, but I have to ask it because other answers arent helping in my case, since I am new to pyqt (switched from tkinter few days ago).I am wondering if is it possib…

Diff multidimensional dictionaries in python

I have two dictionariesa = {home: {name: Team1, score: 0}, away: {name: Team2, score: 0}} b = {home: {name: Team1, score: 2}, away: {name: Team2, score: 0}}The keys never change but I want to get that …

Pandas DatetimeIndex vs to_datetime discrepancies

Im trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite dif…

Slicing a circle in equal segments, Python

I have a set of close of 10,000 points on the sky. They are plotted using the RA (right ascension) and DEC (declination) on the sky. When plotted, they take the shape of a circle. What I would like to …

Pyautogui screenshot. Where does it go? How to save and find later?

I am learning from Al Sweigarts you tube video for automating the boring stuff. I got to the part of taking screenshots. He didnt really explain in his video so I tested things out. I found that it tak…