|
CMake and Allegro 5 |
Rodolfo Lam
Member #16,045
August 2015
|
I'm currently trying to understand CMake and start to use it as my new build system (OK build system generator). I decided to use Allegro as an example of a library to be included in it. I ended up writing a FindAllegro.cmake file flexible enough that it can find Allegro 5 and all current and future Add-ons, as long as the same naming conventions introduced with the 5.2 version still exist. It is still somewhat unfinished as I think it might say everything was found but still fail compilation or will not honor QUIET nor OPTIONAL_COMPONENTS due to some missing logic. But if everything is in its place in your Linux system then it will work. It will find Library names, linker flags and any CFGLAG Allegro might need for you. Here it is in case anyone would like to use it (tested on Mingw-w64 and a Raspberry Pi): 1# FindAllegro
2# -----------
3# Copyright (c) 2018 Rodolfo Lam. All Rights Reserved.
4#
5# Finds the Allegro 5 Library... or at least tries.
6#
7# This will define the following variables::
8#
9# Allegro_FOUND - True if the system has the Allegro library
10# Allegro_VERSION - The version of the Allegro library which was found
11#
12# and the following imported targets::
13#
14# Allegro::Allegro - The Allegro 5 library
15# Allegro::<c> - The requested Allegro Add-on
16
17find_package(PkgConfig REQUIRED)
18pkg_search_module(PC_Allegro QUIET allegro-5)
19
20set(Allegro_FOUND ${PC_Allegro_FOUND})
21
22find_path(Allegro_INCLUDE_DIR
23 NAMES allegro5.h allegro.h
24 PATHS ${PC_Allegro_INCLUDE_DIRS}
25 PATH_SUFFIXES allegro5
26)
27find_library(Allegro_LIBRARY
28 NAMES allegro
29 PATH ${PC_Allegro_LIBRARY_DIRS}
30)
31set(Allegro_VERSION ${PC_ALLEGRO_VERSION})
32
33if(Allegro_FOUND)
34 set(Allegro_LIBRARIES ${Allegro_LIBRARY})
35 set(Allegro_INCLUDE_DIRS ${Allegro_INCLUDE_DIR})
36 set(Allegro_DEFINITIONS ${PC_Allegro_CFLAGS_OTHER})
37endif()
38
39if(Allegro_FOUND AND NOT TARGET Allegro::Allegro)
40 add_library(Allegro::Allegro UNKNOWN IMPORTED)
41 set_target_properties(Allegro::Allegro PROPERTIES
42 IMPORTED_LOCATION "${Allegro_LIBRARIES}"
43 INTERFACE_COMPILE_OPTIONS "${Allegro_DEFINITIONS}"
44 INTERFACE_INCLUDE_DIRECTORIES "${Allegro_INCLUDE_DIRS}"
45 )
46endif()
47
48# Seatch Allegro Add-Ons and include them in the variables
49# will set Allegro_FOUND to 0 if any Add-on is not found.
50foreach(component ${Allegro_FIND_COMPONENTS})
51 pkg_search_module(PC_Allegro_${component} QUIET allegro_${component}-5)
52 if("${PC_Allegro_${component}_FOUND}")
53 find_path(Allegro_${component}_INCLUDE_DIR
54 NAMES allegro_${component}.h
55 PATHS "${PC_Allegro_${component}_INCLUDE_DIRS}"
56 PATH_SUFFIXES allegro5
57 )
58
59 find_library(Allegro_${component}_LIBRARY
60 NAMES allegro_${component} allegro_${component}-5
61 PATH "${PC_Allegro_${component}_LIBRARY_DIRS}"
62 )
63
64 set(Allegro_${component}_FOUND "${PC_Allegro_${component}_FOUND}")
65
66 if(Allegro_${component}_FOUND AND NOT TARGET Allegro::${component})
67 add_library(Allegro::${component} UNKNOWN IMPORTED)
68 set_target_properties(Allegro::${component} PROPERTIES
69 IMPORTED_LOCATION "${Allegro_${component}_LIBRARY}"
70 INTERFACE_COMPILE_OPTIONS "${PC_Allegro_${component}_CFLAGS_OTHER}"
71 INTERFACE_INCLUDE_DIRECTORIES "${Allegro_${component}_INCLUDE_DIR}"
72 )
73 endif()
74
75 mark_as_advanced(Allegro_${component}_INCLUDE_DIR Allegro_${component}_LIBRARY)
76 else()
77 message([FATAL_ERROR] "The Allegro ${component} Add-On was not found!")
78 break()
79 endif()
80endforeach()
81
82include(FindPackageHandleStandardArgs)
83find_package_handle_standard_args(Allegro
84 FOUND_VAR Allegro_FOUND
85 REQUIRED_VARS
86 Allegro_LIBRARY
87 Allegro_INCLUDE_DIR
88 VERSION_VAR Allegro_VERSION
89 HANDLE_COMPONENTS
90)
91
92mark_as_advanced(Allegro_FOUND Allegro_INCLUDE_DIR Allegro_VERSION Allegro_LIBRARY)
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
I'm not familiar with cmake very well, but it looks like this depends on pkg-config to find allegro. pkg-config is optional on Windows, and not very useful except perhaps for niche cases like this. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Rodolfo Lam
Member #16,045
August 2015
|
Yeah... It's one issue I still have, whenever I find a way to reliably find Allegro in Windows I might update it. For now it only works in Windows if you use mingw-w64 and I imagine it will work in Cygwin as well. But not on native MSBuild or using the VS compiler
|
SiegeLord
Member #7,827
October 2006
|
We actually don't create the pkg-config files for MSYS either. I think we should, but it's just not implemented. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Rodolfo Lam
Member #16,045
August 2015
|
Huh... That's interesting... I used pacman to install allegro on msys2 and it included the pkg-config files... So who made those?
|
SiegeLord
Member #7,827
October 2006
|
Huh! Apparently we just don't do it by default, but if you do -DINSTALL_PKG_CONFIG_FILES=on, they'll get enabled. That's neat. Incidentally, if you want, we could add this to Allegro's official source. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Rodolfo Lam
Member #16,045
August 2015
|
Sure no problem at all, include it on the source. Once I finish adding whatever check is missing I'll make a pull request to update it. Thanks!
|
|