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

Add template configuration files and replace all variables before building #320

Closed
waruqi opened this Issue Jan 9, 2019 · 8 comments

Comments

Projects
None yet
1 participant
@waruqi
Copy link
Member

waruqi commented Jan 9, 2019

References

Add configuration files and modify output path

Generate configuation file before building when running $ xmake config

  • set_configdir("$(buildir)/config")
    • Set output directory for the given targets, the default directory: $(buildir)
  • add_configfiles("src/config.h.in")
    • Add configuration files and output to buildir/config/config.h
  • add_configfiles("src/*.h.in", {prefixdir = "subdir"})
    • Add configuration files with prefix directory and output to buildir/config/subdir/*.h
  • add_configfiles("src/config.h")
    • Add configuration files and output to buildir/config/config.h
  • add_configfiles("src/config.h", {filename = "myconfig.h"})
    • Add configuration files and output to buildir/config/myconfig.h
  • add_configfiles("src/(tbox/config.h)")
    • Add configuration files and output to buildir/config/tbox/config.h

Add configuration files and pass arguments

  • add_configfiles("src/config.h", {onlycopy = true})
    • Add configuration files and only copy to buildir/config/config.h, without replacing any variable references or other content
  • add_configfiles("src/config.h", {pattern = "@(.-)@"})
    • Add configuration files and modify the match pattern of variables, the default pattern: %${(.-)}
  • add_configfiles("src/config.h", {variables = {var1 = "xxx", var2 = "yyy"}})
    • Add configuration files and pass the given variables to ${var1}, ${var2}

Pass macro definitions and option status

  • Support options
  • set_configvar("VAR", 1)
    • ${VAR} => 1
    • ${define VAR} => #define VAR 1 or #undef VAR
    • ${default VAR 0} => 1 or 0

Deprecated Apis:

- set_config_header()
- add_defines_h()

Set the template variables for configuration file.

  • set_configvar("var1", "xxx")
  • set_configvar("zzz.var3", "yyy")
#define XXXX ${var1}
#define YYY_${var2}_ENABLE
#define ZZZ_${zzz.var3} 0

Builtin variables

  • ${VERSION} -> 1.6.3
  • ${VERSION_MAJOR} -> 1
  • ${VERSION_MINOR} -> 6
  • ${VERSION_ALTER} -> 3
  • ${VERSION_BUILD} -> set_version("1.6.3", {build = "%Y%m%d%H%M"}) -> 201902031421
  • ${PLAT} and ${plat} -> MACOS and macosx
  • ${ARCH} and ${arch} -> ARM and arm
  • ${MODE} and ${mode} -> DEBUG/RELEASE and debug/release
  • ${DEBUG} and ${debug} -> 1 or 0
  • ${OS} and ${os} -> IOS or ios

Features

  • Custem macro variables of template
  • Support global root configuration file
  • Support multiple configuration/template files for targets
  • Custom output directory and file

@waruqi waruqi added the new feature label Jan 9, 2019

@waruqi waruqi added this to the v2.2.4 milestone Jan 9, 2019

@waruqi waruqi added the in progress label Feb 2, 2019

@waruqi waruqi changed the title Add common configuration and template files in configuration and building stage Add template configuration files and replace all variables before building Feb 2, 2019

@waruqi waruqi pinned this issue Feb 2, 2019

@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Examples

-- set global config variables
set_configvar("VAR1", get_config("var1"))
set_configvar("VAR2", get_config("var2"))

target("test")
    set_kind("binary")
    add_files("main.c")

    -- only set variable to test target
    set_configvar("module", "test")

    -- set config output directory
    set_configdir("$(buildir)/config")

    -- add config files
    add_configfiles("config.h.in", {variables = {hello = "xmake"}, prefixdir = "header"})
    add_configfiles("*.man", {copyonly = true})

    -- search the generated config.h
    add_includedirs("$(buildir)/config/header")

config.h.in

#define VAR1 "${VAR1}"
#define VAR2 "${VAR2}"
#define HELLO "${HELLO}"
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Examples

set_configvar("VAR", get_config("var"))
target("test")
    set_kind("binary")
    add_files("main.c")
    add_configfiles("config.h.in", {pattern = "@(.-)@"})
    add_includedirs("$(buildir)")

config.h.in

#define VAR "@VAR@"
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Pass boolean option status to the macro definitions

option("foo")
    set_default(true)
    set_description("Enable Foo")
option_end()

if has_config("foo") then
    set_configvar("FOO_ENABLE", 1) -- or pass true
    set_configvar("FOO_STRING", "foo")
end

config.h.in

${define FOO_ENABLE}
${define FOO_STRING}

config.h

#define FOO_ENABLE 1
#define FOO_STRING "foo"

Or

/* #undef FOO_ENABLE */
/* #undef FOO_STRING */
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Pass string option status to the macro definitions

option("foo")
    set_default("foo")
    set_description("The Foo Info")
option_end()

if has_config("foo") then
    set_configvar("FOO_STRING", get_config("foo"))
end

config.h.in

${define FOO_STRING}

config.h

#define FOO_STRING "foo"

Or

/* #undef FOO_ENABLE */
/* #undef FOO_STRING */
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Attach option status and the macro definitions to target

option("foo")
    set_default(true)
    set_description("Enable Foo")
    set_configvar("FOO_ENABLE", 1) -- or pass true
    set_configvar("FOO_STRING", "foo")

target("test")
    add_configfiles("config.h.in")

    -- if foo enabled -> add FOO_ENABLE and FOO_STRING definitions
    add_options("foo") 

config.h.in

${define FOO_ENABLE}
${define FOO_STRING}

config.h

#define FOO_ENABLE 1
#define FOO_STRING "foo"

Or

/* #undef FOO_ENABLE */
/* #undef FOO_STRING */
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 3, 2019

Uses the builtin version variables

target("test")
    set_version("1.6.3")
    add_configfiles("config.h.in")

config.h.in

#define CONFIG_VERSION "${VERSION}"
#define CONFIG_VERSION_MAJOR ${VERSION_MAJOR}
#define CONFIG_VERSION_MINOR ${VERSION_MINOR}
#define CONFIG_VERSION_ALTER ${VERSION_ALTER}
#define CONFIG_VERSION_BUILD ${VERSION_BUILD}

config.h

#define CONFIG_VERSION "1.6.3"
#define CONFIG_VERSION_MAJOR 1
#define CONFIG_VERSION_MINOR 6
#define CONFIG_VERSION_ALTER 3
#define CONFIG_VERSION_BUILD 201902031401
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Feb 18, 2019

Add default variables

config.h.in

HAVE_SSE2 equ ${default VAR_HAVE_SSE2 0}

default config.h

HAVE_SSE2 equ 0

After set_configvar("HAVE_SSE2", 1)

config.h

HAVE_SSE2 equ 1
@waruqi

This comment has been minimized.

Copy link
Member Author

waruqi commented Mar 30, 2019

#342 Add some builtin help functions for includes(), e.g. check_xxx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.