1# @title cmake macro for pkgconfig files generation 2# @brief generate a .pc package config file with a given name 3# @author Adrien Devresse 4 5include(DefineInstallationPaths REQUIRED) 6include(CMakeMacroParseArguments REQUIRED) 7include(CMakeStringHelpers REQUIRED) 8 9 10SET(CMAKE_PKGCONFIG_TEMPLATE "prefix=@PREFIX@ 11exec_prefix=@PREFIX@ 12libdir=@LIBDIR_VAR 13includedir=@INCLUDE_VAR@ 14 15Name: @NAME_PROJECT@ 16Description: @DESCRIPTION_PROJECT@ 17Version: @VERSION_PROJECT@ 18URL: @URL_PROJECT@ 19Requires: @REQUIRES_PROJECT@ 20Conflicts: @CONFLICTS_PROJECT@ 21Libs: @LIBS_PROJECT@ 22Libs.private: @LIBS_PRIVATE_PROJECT@ 23Cflags: @CFLAGS_PROJECT@ 24") 25 26 27SET(CMAKE_PKGCONFIG_TEMPLATE_BASE " 28prefix=@PREFIX@ 29exec_prefix= \\\${prefix} 30libdir= @LIBDIR_VAR@ 31includedir=@INCLUDE_VAR@ 32 33Name: @NAME_PROJECT@ 34Description: @DESCRIPTION_PROJECT@ 35Version: @VERSION_PROJECT@ 36Requires: @REQUIRES_PROJECT@ 37Libs: @LIBS_PROJECT@ 38Cflags: @CFLAGS_PROJECT@ 39" ) 40 41 42LIST(APPEND CMAKE_PKGCONFIG_TEMPLATE_BASE_PATTERN "@PREFIX@" "@LIBDIR_VAR@" 43 "@INCLUDE_VAR@" "@NAME_PROJECT@" "@DESCRIPTION_PROJECT@" "@VERSION_PROJECT@" 44 "@REQUIRES_PROJECT@" "@LIBS_PROJECT@" "@CFLAGS_PROJECT@") 45 46 47# main function to use 48# FORMAT : add_PkgConfigFile_for_Library("string_filename.pc" target_library 49# [DESCRIPTION] "description of the pkgconfig files" 50# [HEADER_DIRS] dir1, dir2 51# [REQUIRES] req1 req 2 ) # list of dir to include in $prefix/include/, ex : $prefix/include/dir1 52# the pc file is produced in the ${CMAKE_CURRENT_BINARY_DIR} directory 53 54function(add_PkgConfigFile_for_Library) 55 PARSE_ARGUMENTS(PKGCONFIGFILE 56 "HEADER_DIRS;DESCRIPTION;REQUIRES;CFLAGS" 57 "" 58 ${ARGN} 59 ) 60 61 LIST(GET PKGCONFIGFILE_DEFAULT_ARGS 0 pkgconfig_filename) 62 LIST(GET PKGCONFIGFILE_DEFAULT_ARGS 1 lib_target) 63 LIST(GET PKGCONFIGFILE_DESCRIPTION 0 description) 64 65 get_target_property(library_name ${lib_target} OUTPUT_NAME) 66 get_target_property(library_version ${lib_target} VERSION) 67 68 set(pkgconfig_prefix "${CMAKE_INSTALL_PREFIX}") 69 set(pkgconfig_libdir_var "\\\${prefix}/lib${LIB_SUFFIX}") 70 set(pkgconfig_include_var "\\\${prefix}/include") 71 72 set(pkgconfig_linkflags "-l${library_name} -L\\\${libdir}") 73 set(pkgconfig_name "${pkgconfig_filename}") 74 set(pkgconfig_version "${library_version}") 75 set(pkgconfig_description "pkgconfig file for ${library_name}") 76 set(pkgconfig_requires " ") 77 set(pkgconfig_cflags "") 78 79 IF(PKGCONFIGFILE_REQUIRES) 80 FOREACH(req ${PKGCONFIGFILE_REQUIRES}) 81 set(pkgconfig_requires "${pkgconfig_requires} ${req}") 82 ENDFOREACH(req PKGCONFIGFILE_REQUIRES) 83 ENDIF(PKGCONFIGFILE_REQUIRES) 84 85 IF(PKGCONFIGFILE_CFLAGS) 86 FOREACH(req ${PKGCONFIGFILE_CFLAGS}) 87 set(pkgconfig_cflags "${pkgconfig_cflags} ${req}") 88 ENDFOREACH(req PKGCONFIGFILE_CFLAGS) 89 ENDIF(PKGCONFIGFILE_CFLAGS) 90 91 IF(PKGCONFIGFILE_HEADER_DIRS) 92 FOREACH(dir ${PKGCONFIGFILE_HEADER_DIRS}) 93 set(pkgconfig_includedir "${pkgconfig_includedir} -I\\\${includedir}/${dir}") 94 ENDFOREACH(dir PKGCONFIGFILE_HEADER_DIRS) 95 ELSE(PKGCONFIGFILE_HEADER_DIRS) 96 set(pkgconfig_includedir " -I\\\${includedir}") 97 ENDIF(PKGCONFIGFILE_HEADER_DIRS) 98 99 IF(description) 100 set(pkgconfig_description "${description}") 101 102 ENDIF(description) 103 104 set(pkgconfig_cflags "${pkgconfig_cflags} ${pkgconfig_includedir} ") 105 106 LIST(APPEND pkgconfig_list_var ${pkgconfig_prefix} ${pkgconfig_libdir_var} 107 ${pkgconfig_include_var} ${pkgconfig_name} ${pkgconfig_description} 108 ${pkgconfig_version} ${pkgconfig_requires} ${pkgconfig_linkflags} ${pkgconfig_cflags}) 109 replace_all_occurence(pc_file_content ${CMAKE_PKGCONFIG_TEMPLATE_BASE} 110 LIST_PATTERN ${CMAKE_PKGCONFIG_TEMPLATE_BASE_PATTERN} LIST_REPLACER ${pkgconfig_list_var}) 111 112 SET(filename "${CMAKE_CURRENT_BINARY_DIR}/${pkgconfig_filename}") 113 FILE(WRITE ${filename} "${pc_file_content}" ) 114 message(STATUS "generate pkgconfig file for ${lib_target} under ${filename}") 115endfunction(add_PkgConfigFile_for_Library) 116 117