# ports / Makefile / check if default compiler is GCC and version is 4.2+



## log69 (Oct 5, 2013)

Dear Members,

I'd like to ask your help. I'm a developer and maintainer of a port and I intend to give different compile options for different compilers when building my software. I'm using the OpenMP feature of GCC that is available from GCC version 4.2 and above.

I would like to know how I could determine in my Makefile whether the default compiler is GCC and it's version is >= 4.2 ?

I'd like to go with the preffered standard way.

Is there any chance or does it make any sense to try to support the compile of GCC version lower than 4.2? If not, then I reckon it would be enough for me to check against GCC only without its version. If so, then would this be enough? :

.if ${CC} == "gcc"


Thanks.


----------



## ShelLuser (Oct 5, 2013)

Hi there,

First of all I'd like to recite the moderators by mentioning that this forum is mostly aimed at end users and not so much developers or porters. Thankfully this doesn't mean that we're not allowed to address these more advanced or specific topics, but be aware that you'll most likely reach a larger audience when using the freebsd-ports mailinglist.



			
				log69 said:
			
		

> I would like to know how I could determine in my Makefile whether the default compiler is GCC and it's version is >= 4.2 ?
> 
> I'd like to go with the preffered standard way.


Interesting question.

The first place to start looking when dealing with the ports collection is the porters handbook but after looking this up I also noticed that it doesn't mention much about the compilers other that the issue of respecting cc and cxx variables.

Now, one of the cool things about FreeBSD is that all its major components (world/kernel, documentation and the ports collection) share a specific methodology: the usage of include files. So in case something isn't fully made clear to you in the documentation then this is the defacto place to turn to.

In the case of the ports these reside in /usr/ports/Mk and here you'll find bsd.gcc.mk which contains all you need to address any GCC related issues:


```
# If your port needs a specific (minimum) version of GCC, you can easily
# specify that with a USE_GCC= statement.  Unless absolutely necessary
# do so by specifying USE_GCC=X.Y+ which requests at least GCC version
# X.Y.  To request a specific version omit the trailing + sign.
```
Now I realize that you're not looking for specifying what GCC version to use but merely wish to do a check yourself. Well; that is also something where this file may be able to help out. If you study it further you'll come across this:


```
# Initialize _GCC_FOUND${v}.  In parallel, check if USE_GCC points to a
# valid version to begin with.
.for v in ${GCCVERSIONS}
[B]. if exists(${LOCALBASE}/bin/gcc${_GCCVERSION_${v}_V:S/.//})[/B]
_GCC_FOUND${v}= port
. elif ${OSVERSION} >= ${_GCCVERSION_${v}_L} && ${OSVERSION} < ${_GCCVERSION_${v
.  if exists(/usr/bin/gcc)
_GCC_FOUND${v}= base
.  endif
. endif
```
Now, I'm not 100% sure here but it looks to me as if the ports system framework (to give it a name) doesn't necessarily deal with options which can tell you which GCC versions can be used. It seems more aimed at allowing you to specify a specific GCC version to use after which it will try to meet that request.

So I'm wondering if you couldn't simply use the routine as it is used here to check for a specific version to be present?



			
				log69 said:
			
		

> Is there any chance or does it make any sense to try to support the compile of GCC version lower than 4.2? If not, then I reckon it would be enough for me to check against GCC only without its version.


Now, this is a rough guess on my part but I don't think there is. GCC 4.2 has been the default compiler in FreeBSD for quite some time now and if you look at the ports collection then you'll notice that although lang/gcc34 still exists it's marked as DEPRECATED with a direct reference to lang/gcc46 and up.



			
				log69 said:
			
		

> If so, then would this be enough? :
> 
> .if ${CC} == "gcc"


I'm not 100% sure but I don't think this will work because CC can also contain compiler flags.

That is mentioned in the section I mentioned earlier about respecting cc and cxx variables.

So, as mentioned earlier, my assumption here is that your best option is to check the version manually. Or better yet simply assume 4.2 and up because 4.2 is the default version as supplied by the base system.

Hope this can help.


----------



## log69 (Oct 5, 2013)

Thank you for the thorough answer.

For the last paragraph, then I also think that I won't bother with the version numbers. But how to check then for gcc if it's default or not?

I try not to do hacks like grep the value of the CC variable and such.


----------

