# What does this code do?



## PMc (Jul 21, 2019)

In /usr/ports/editors/emacs/Makefile I find this piece of code:

```
220 # has graphics/ImageMagick been compiled with OPENMP?
    221 .if ${PORT_OPTIONS:MMAGICK} && ${:!${GREP} -sc " \-fopenmp " ${LOCALBASE}/libdata/pkgconfig/ImageMagick.pc || true!} == "1"
    222 USES+=  compiler:openmp
    223 .endif
```

Does anybody understand what this is supposed to do?


----------



## Eric A. Borisch (Jul 21, 2019)

Just what it says: it checks for the OpenMP option in ImageMagick’s pkgconf file, and turns on OpenMP support for emacs if it is enabled for ImageMagick. (Likely to fix a linking error if compiled without OpenMP, but that’s just a guess on my part.)

Explicitly, it runs grep in silent and count mode, and sees if it returns “1”. The || true is there to not throw an error since we don’t want to bail if it fails. (If grep doesn’t find -fopenmp.)


----------



## PMc (Jul 21, 2019)

Eric A. Borisch said:


> Just what it says: it checks for the OpenMP option in ImageMagick’s pkgconf file,



Well, but ImageMagick is not yet built when this code runs. The user will decide at a later time if they want to use OpenMP.

Furthermore, when setting the OPENMP option in ImageMagick, I see this:

```
checking for cc option to support OpenMP... unsupported
```

And /usr/ports/graphics/ImageMagick6/Makefile contains this:

```
46     USES=           compiler:c++11-lang cpe gettext-runtime gmake gnome libtool:keepla
[...]
230     .if ${PORT_OPTIONS:MOPENMP}
231     . if ${_IMAGEMAGICK_THREADS} == "no"
232     IGNORE=OpenMP requires threads${_IMAGEMAGICK_THREADS_IGNORE_MSG}
233     . else
234     CONFIGURE_ARGS+=        --enable-openmp
235     USES+=          compiler:openmp
236     . endif
```

It seems, setting two compilers does not to work, anyway.

Just noticed: trying to ask same questions on the freebsd-ports list, posting was denied. (probably considered hate-speech, as nowadays is the usual way to handle unwelcome questions.)


----------



## Eric A. Borisch (Jul 21, 2019)

ImageMagick _will_ be installed by the time this section is executed if the user has selected ImageMagick support for emacs. (Unless the emacs port is broken, but I see lines in the Makefile for depending on libMagickCore...)

Now the ImageMagick port may need some touch-up to get OpenMP support working, if you’re finding it won’t detect OpenMP support. It looks like Clang’s OpenMP support will be part of base in 13, so ports will hopefully no longer need The openmp option for compiler in USES for configure tests to find OpenMP support (this is conjecture on my part.)

[edit: typo; de-auto-emoji]


----------



## Eric A. Borisch (Jul 21, 2019)

It looks to me like the OpenMP build support is broken wrt. ImageMagick6 from a quick test of building, but I don't have time to dig into it further this morning. ImageMagick7 seems to detect it correctly.


----------



## PMc (Jul 21, 2019)

Eric A. Borisch said:


> ImageMagick _will_ be installed by the time this section is executed if the user has selected ImageMagick support for emacs.



Yes, but it does it at a later point in time!
Lets suppose ImageMagick is already installed, and is complied with OpenMP support. Then the execution of `USES+=compiler:openmp` in the makefile will effectively switch the compiler to gcc8 (see /usr/ports/Mk/Uses/compiler.mk). And now lets look at the build log:

```
===>   emacs-26.1_6,3 depends on file: /usr/local/sbin/pkg - found
===> Fetching all distfiles required by emacs-26.1_6,3 for building
===>  Extracting for emacs-26.1_6,3
=> SHA256 Checksum OK for emacs-26.1.tar.xz.
===>  Patching for emacs-26.1_6,3
===>  Applying FreeBSD patches for emacs-26.1_6,3
===>   emacs-26.1_6,3 depends on executable: gmake - found
===>   emacs-26.1_6,3 depends on executable: makeinfo - found
===>   emacs-26.1_6,3 depends on package: pkgconf>=1.3.0_1 - found
===>   emacs-26.1_6,3 depends on executable: update-desktop-database - found
===>   emacs-26.1_6,3 depends on executable: gtk-update-icon-cache - found
===>   emacs-26.1_6,3 depends on executable: gcc8 - not found
Making GCC 8.3.0 for x86_64-portbld-freebsd11.3 [c,c++,objc,fortran]
```

At this point we start to build gcc8, so at this point the code in the Makefile has been executed, and the decision to use or not to use an OpenMP-capable compilier has already been made!

Then the building of the gcc8 (and all its prereqs) follows, and afterwards we get back to the build of emacs:

```
===>   emacs-26.1_6,3 depends on executable: gcc8 - found
===>   Returning to build of emacs-26.1_6,3
===>   emacs-26.1_6,3 depends on file: /usr/local/bin/as - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/ice.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/sm.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/x11.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xcb.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xext.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xfixes.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xinerama.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xmu.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xrandr.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xt.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xft.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xrender.pc - found
===>   emacs-26.1_6,3 depends on file: /usr/local/libdata/pkgconfig/xpm.pc - found
===>   emacs-26.1_6,3 depends on shared library: libgif.so - found (/usr/local/lib/libgif.so)
===>   emacs-26.1_6,3 depends on shared library: libgnutls.so - found (/usr/local/lib/libgnutls.so)
===>   emacs-26.1_6,3 depends on shared library: libfreetype.so - found (/usr/local/lib/libfreetype.so)
===>   emacs-26.1_6,3 depends on shared library: libfontconfig.so - found (/usr/local/lib/libfontconfig.so)
===>   emacs-26.1_6,3 depends on shared library: libjansson.so - found (/usr/local/lib/libjansson.so)
===>   emacs-26.1_6,3 depends on shared library: liblcms2.so - found (/usr/local/lib/liblcms2.so)
===>   emacs-26.1_6,3 depends on shared library: libMagickCore-6.so - found (/usr/local/lib/libMagickCore-6.so)
```

And at this point finally we get to the prereq ImageMagick6 - and here we would start to build it (if it weren't there), and here the user would be asked for the compile-time options.

Borttomline is: you cannot, in any port's makefile, query the filesystem in $LOCALBASE for the options of a prerequisite port, because it need not yet be there when the query is made.

The biggest problem here is, that this will produce surprizingly changing code - the build result will be different on the first and on subsequent builds - and that's very evil.



> Now the ImageMagick port may need some touch-up to get OpenMP support working, if you’re finding it
> won’t detect OpenMP support.



Yepp, it seems the coding in that makefile is not doing what was intended.



> It looks like Clang’s OpenMP support will be part of base in 13,



It seems 11.3 has this support already, 'cause I removed all strange stuff from the Makefiles and it still builds, with clang8. (Didn't test if it is functional, but at least it links.)
So we can probably just sit this out and wait until 12.0 is out-of-support, and then remove all that stuff.

Nevertheless, two issues remain:
1. people who are coding ports' makefiles should remember that it is always troublesome to grep into $LOCALBASE, for whatever, as this will lead to erratically changing build results depending on the sequence of port's installations.
2. The code in /usr/ports/Mk might do a little more testing for variable settings that do not make real sense.



Eric A. Borisch said:


> It looks to me like the OpenMP build support is broken wrt. ImageMagick6 from a quick test of building, but I don't have time to dig into it further this morning. ImageMagick7 seems to detect it correctly.



It did at least detect, compile and link for me, with 11.3:

```
===>  Configuring for ImageMagick6-6.9.10.47,1
[...]
checking for cc option to support OpenMP... -fopenmp
checking for pthread_join using cc -pthread ... yes
checking whether cc is Clang... (cached) yes
```

But I'm in the mid of an upgrade cycle
1. upgrade the OS 11.2 -> 11.3
2. rebuild all the ports from the old portstree (2019Q2) with the new OS
3. update the portstree
currently in step 2. I'm doing it that way because I have been burned often enough by such kind of happeningsl, so I'm following the "_change only one parameter at a time_" pragma.


----------

