Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cmake build for git submodules on unix #179

Merged
merged 1 commit into from
Jul 11, 2021
Merged

Fix cmake build for git submodules on unix #179

merged 1 commit into from
Jul 11, 2021

Conversation

crvarner
Copy link
Contributor

@crvarner crvarner commented Jul 9, 2021

CMake build when including SDL_image as git submodule was broken for unix:

$ make
[  1%] Generating pnglibconf.c
[  2%] Generating pnglibconf.out
/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37/pnglibconf.c:34:11: fatal error: zlib.h: No such file or directory
   34 | # include <zlib.h>
      |           ^~~~~~~~
compilation terminated.
CMake Error at scripts/genout.cmake:78 (message):
  Failed to generate
  /home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37/pnglibconf.out.tf1

looking at the scripts/genout.cmake created by cmake, you can see how the includes are generated

set(INCDIR "/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37")
...
set(ZLIBINCDIR "external/zlib-1.2.11")    <--- this comes from ZLIB_INCLUDE_DIR
...
  set(INCLUDES "-I${INCDIR}")
  if(ZLIBINCDIR)
    foreach(dir ${ZLIBINCDIR})
      list(APPEND INCLUDES "-I${dir}")
    endforeach()
  endif()

which results in the following compile commands while compiling the generated file pnglibconf.c:

cd /home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37
/usr/bin/cc' '-E'
'-I/home/cvarner/workspace/game-template/external/SDL_image/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37'
'-Iexternal/zlib-1.2.11'
'-DPNGLIB_LIBNAME=PNG16_0'
'-DPNGLIB_VERSION=1.6.37'
'-DSYMBOL_PREFIX='
'-DPNG_NO_USE_READ_MACROS'
'-DPNG_BUILDING_SYMBOL_TABLE'
'/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37/pnglibconf.c

Note that the relative include "external/zlib-1.2.11" resolves to /home/cvarner/workspace/game-template/external/SDL_image/external/libpng-1.6.37/external/zlib-1.2.11 instead of the zlib source, causing the original error.

Updating the top level CMakeLists.txt to use an absolute path results in the following compile command, which is better

'/usr/bin/cc'
'-E'
'-I/home/cvarner/workspace/game-template/external/SDL_image/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/external/SDL_image/external/zlib-1.2.11'
'-DPNGLIB_LIBNAME=PNG16_0' '-DPNGLIB_VERSION=1.6.37' '-DSYMBOL_PREFIX=' '-DPNG_NO_USE_READ_MACROS' '-DPNG_BUILDING_SYMBOL_TABLE'
'/home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37/pnglibconf.c'

We are still missing zlib's output directory though so we get the following error:

/home/cvarner/workspace/game-template/external/SDL_image/external/zlib-1.2.11/zlib.h:34:10: fatal error: zconf.h: No such file or directory
   34 | #include "zconf.h"
      |          ^~~~~~~~~
compilation terminated.
CMake Error at scripts/genout.cmake:79 (message):
  Failed to generate
  /home/cvarner/workspace/game-template/build/external/SDL_image/external/libpng-1.6.37/pnglibconf.out.tf1

zlibs include directories should include both the original source and the binary dir. This PR does that with 2 changes:

  1. CMake: including current src directory into include path madler/zlib#218 is a long standing zlib issue with a known solution CMake: use CMAKE_CURRENT_SOURCE_DIR, *not* top-level src dir. Fixes #218 madler/zlib#219 that only appears to remain unfixed because the repo hasn't been updated since 2017.

  2. With (1) fixed, ZLIB_INCLUDE_DIR can be set using the INCLUDE_DIRECTORIES property of the zlib target created by adding the zlib subdirectory. This variable now correctly contains all include directories resulting in the following compile command:

'/usr/bin/cc' '-E'
'-I/home/cvarner/workspace/game-template/external/SDL_image_fork/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image_fork/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image_fork/external/libpng-1.6.37'
'-I/home/cvarner/workspace/game-template/build/external/SDL_image_fork/external/zlib-1.2.11'
'-I/home/cvarner/workspace/game-template/external/SDL_image_fork/external/zlib-1.2.11'
'-DPNGLIB_LIBNAME=PNG16_0'
'-DPNGLIB_VERSION=1.6.37'
'-DSYMBOL_PREFIX='
'-DPNG_NO_USE_READ_MACROS'
'-DPNG_BUILDING_SYMBOL_TABLE'
'/home/cvarner/workspace/game-template/build/external/SDL_image_fork/external/libpng-1.6.37/pnglibconf.c'

The build succeeds with no issues from there on

- set ZLIB_INCLUDE_DIR from target properties rather than hardcoding
@sezero
Copy link
Contributor

sezero commented Jul 11, 2021

Noticed this one lat, sorry. Applying.

@sezero sezero merged commit d5cc8b8 into libsdl-org:main Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants