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

Support GenerateExportHeader #6088

Closed
star-hengxing opened this issue Jan 17, 2025 · 2 comments
Closed

Support GenerateExportHeader #6088

star-hengxing opened this issue Jan 17, 2025 · 2 comments

Comments

@star-hengxing
Copy link
Contributor

Is your feature request related to a problem? Please describe.

https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html

Describe the solution you'd like

N/A

Describe alternatives you've considered

No response

Additional context

No response

@waruqi
Copy link
Member

waruqi commented Jan 23, 2025

试了下,就为了生成这个?这玩意自己按需定义下,也没几行代码 = =

$ cat include/my_lib_export.h

#ifndef MY_LIB_EXPORT_H
#define MY_LIB_EXPORT_H

#ifdef MY_LIB_STATIC_DEFINE
#  define MY_LIB_EXPORT
#  define MY_LIB_NO_EXPORT
#else
#  ifndef MY_LIB_EXPORT
#    ifdef my_lib_EXPORTS
        /* We are building this library */
#      define MY_LIB_EXPORT __attribute__((visibility("default")))
#    else
        /* We are using this library */
#      define MY_LIB_EXPORT __attribute__((visibility("default")))
#    endif
#  endif

#  ifndef MY_LIB_NO_EXPORT
#    define MY_LIB_NO_EXPORT __attribute__((visibility("hidden")))
#  endif
#endif

#ifndef MY_LIB_DEPRECATED
#  define MY_LIB_DEPRECATED __attribute__ ((__deprecated__))
#endif

#ifndef MY_LIB_DEPRECATED_EXPORT
#  define MY_LIB_DEPRECATED_EXPORT MY_LIB_EXPORT MY_LIB_DEPRECATED
#endif

#ifndef MY_LIB_DEPRECATED_NO_EXPORT
#  define MY_LIB_DEPRECATED_NO_EXPORT MY_LIB_NO_EXPORT MY_LIB_DEPRECATED
#endif

#if 0 /* DEFINE_NO_DEPRECATED */
#  ifndef MY_LIB_NO_DEPRECATED
#    define MY_LIB_NO_DEPRECATED
#  endif
#endif

#endif /* MY_LIB_EXPORT_H */

@waruqi
Copy link
Member

waruqi commented Jan 27, 2025

没必要为此单独搞个头文件,复用现有的 add_configfiles 进行扩展下自定义就行了。

试下这个 patch #6119

只需要在 config.h.in 里定义

${define_export MYLIB}

config.h 就可以展开生成

#ifdef MYLIB_STATIC
#  define MYLIB_EXPORT
#else
#  if defined(_WIN32)
#    define MYLIB_EXPORT __declspec(dllexport)
#  elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
#    define MYLIB_EXPORT __attribute__((visibility("default")))
#  else
#    define MYLIB_EXPORT
#  endif
#endif

当然,如果内置的生成不满足要求,可以完全自定义,我改进了 add_configfiles ,可以自定义 preprocessor 去完全重写内置的一些预处理。

具体详情看:#6120

例如:

target("test")
    set_kind("binary")
    add_files("main.c")
    add_configfiles("config.h.in", {
        preprocessor = function (preprocessor_name, name, value, opt)
            if preprocessor_name == "define_export" then
                    value = ([[#ifdef %s_STATIC
#  define %s_EXPORT
#else
#  if defined(_WIN32)
#    define %s_EXPORT __declspec(dllexport)
#  elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
#    define %s_EXPORT __attribute__((visibility("default")))
#  else
#    define %s_EXPORT
#  endif
#endif
]]):format(name, name, name, name, name)
                return value
            end
        end})

@waruqi waruqi closed this as completed Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants