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

"phony target" request #68

Closed
ghost opened this issue Apr 8, 2017 · 11 comments
Closed

"phony target" request #68

ghost opened this issue Apr 8, 2017 · 11 comments

Comments

@ghost
Copy link

ghost commented Apr 8, 2017

Feature request!

The idea of phony target comes from makefile. It is a target that do not actually build anything.

I think it has meanings to add this feature. For example, I have testadd, testsub... So a phony target testsuite might be needed to build all tests together then put them into a certain directory. It's also possible in xmake today but a little troublesome.

There is also an another feature request. That is, $(xmake). makefile has $(MAKE) to let script use make itself. I found it's also useful to add such.

Oh, finally I suddenly get an idea. To let commandline pass vars to script. I think we could do things as below at last:

target("greet")
    set_phony(true)
    on_build(function (target)    -- on_build? or another better?
        print("Hello, $(name)")
    end)
$ xmake build greet name=World
Hello, World
@waruqi
Copy link
Member

waruqi commented Apr 9, 2017

You can add a phony target without target kind to only add target's building deps. For example:

target("testsuite")
    set_default(false)
    add_deps("testprogadd", "testprogsub", "testprogmul", "testprogdiv", "testprogmod")

And run command to build these tests:

$ xmake build testsuite

xmake also support $(name) variable, for example:

option("name")
    set_showmenu(true)
    set_default("world")

target("test")
    set_kind("binary")
    add_files("*.c")
    after_build(function (target)
        print("hello $(name)!")
    end)

So the default value of this variable $(name) is world and you can also modify it:

$ xmake f --name=xmake

The result is:

hello xmake!

And you also can add $(name) variable to files:

target("test")
    set_kind("binary")
    add_files("$(name)/*c")
    add_links("$(name)_$(arch)")
    ...

Please checkout the lastest commit 23ffe01, I fixed print("$(name)") problem and supported phony target without target kind.

Thanks!

@ghost
Copy link
Author

ghost commented Apr 9, 2017

Oh, sounds interesting. What about $(xmake)?

@ghost
Copy link
Author

ghost commented Apr 9, 2017

Sorry but I couldn't understand this code you wrote:

option("name")
    set_showmenu(true)
    set_default("world")

target("test")
    set_kind("binary")
    add_files("*.c")
    after_build(function (target)
        print("hello $(name)!")
    end)

Will target test do building? Why uses after_build?

Also, use xmake f --name=xmake seems to be not so direct. Why not directly pass it when building?

Another question, could I do something in phony target? For example, copying something?

@waruqi
Copy link
Member

waruqi commented Apr 9, 2017

Sorry, I couldn't understand what you mean.

If you want to pass variable to print("$(name)"), you can define option("name") and config it xmake f --name=xxx. I use after_build() to describe the usage of variable. It is only an example.

If you want to use make command in the xmake.lua, you can use os.run("make") or os.exec("make"), for example:

target("test")
    on_build(function (target)

        -- quietly run make
        os.run("make")
 
        -- run make and output info
        os.exec("make")
    end)

For second question, you can copying something on the custom build or other scripts.

target("testsuite")
    set_default(false)
    add_deps("testprogadd", "testprogsub", "testprogmul", "testprogdiv", "testprogmod")

    on_build(function (target)
        os.cp("file1", "file2", "dir/*", "to_destfile_or_dir")
    end
    after_build(function (target)
        os.cp("file1", "file2", "dir/*", "to_destfile_or_dir")
    end
    on_package(function (target)
        os.cp("file1", "file2", "dir/*", "to_destfile_or_dir")
    end

@ghost
Copy link
Author

ghost commented Apr 9, 2017

I do not mean to use make in script. What I said is xmake: $(xmake). If someone has installed xmake into other dir not in PATH environment var, just use os.run("xmake") is not enough. It needs to get args[0] passed to xmake main program in script.

$ /path/to/xmake
# args[0] would be absolute path what is I wanna to get

@waruqi
Copy link
Member

waruqi commented Apr 9, 2017

You can get the absolute path from $(programdir)/xmake, for example:

os.run("$(programdir)/xmake")

@ghost
Copy link
Author

ghost commented Apr 9, 2017

😄

@ghost ghost closed this as completed Apr 9, 2017
@ghost
Copy link
Author

ghost commented Apr 16, 2017

Are you sure it really works?

target("test")
    on_build(function (target)
        os.exec("$(programdir)/xmake --version")
    end)
    on_clean(function (target)
        os.exec("$(programdir)/xmake --version")
    end)

got:

error: invalid variable: $(programdir)

By the way, if there is not on_clean like

target("test")
    on_build(function (target)
        os.exec("$(programdir)/xmake --version")
    end)

got:

error: bad argument #1 to 'pairs' (table expected, got nil)

Why there must be on_clean?

The reason why I wanna use xmake inside is because I do not like add_subdirs. I found it getting into the way. For example, I have this path tree:

.
├── winpty
│   ├── ...
│   └── src
│       ├── ...
│       └── xmake.lua (1)
└── xmake.lua (2)

And xmake.lua (1) is below:

set_xmakever("2.1.3")
set_project("winpty")
set_languages("ansi","gnuxx11")
add_defines("_WIN32_WINNT=0x0501")
target("winpty")
    set_kind("shared")
    add_headers("include/*")
    add_includedirs("include","gen")
    add_defines("COMPILING_WINPTY_DLL")
    add_links("advapi32","user32")
    add_files(
                "libwinpty/AgentLocation.cc",
                "libwinpty/winpty.cc",
                "shared/BackgroundDesktop.cc",
                "shared/Buffer.cc",
                "shared/DebugClient.cc",
                "shared/GenRandom.cc",
                "shared/OwnedHandle.cc",
                "shared/StringUtil.cc",
                "shared/WindowsSecurity.cc",
                "shared/WindowsVersion.cc",
                "shared/WinptyAssert.cc",
                "shared/WinptyException.cc",
                "shared/WinptyVersion.cc")
    before_build(function (target)
        os.cd("shared")
        os.exec("UpdateGenVersion.bat")
        os.cd("..")
    end)

Sorry, it's little long. Can be seen at xmake.lua

Please have a mind at before_build. There I used os.cd.

I try to write xmake.lua (2) as this:

add_subdirs(path.join("winpty","src"))

But it fails with cannot change directory

So, when building subdirs, xmake does not enter it?

How to solve this? How to build submodule with xmake? Would be better if there is add_submodules?

@ghost ghost reopened this Apr 16, 2017
@waruqi
Copy link
Member

waruqi commented Apr 16, 2017

I'm sorry, I forgot to add $(programdir) variable and I have added it and $(xmake) on commit 057493c. You can run command:

os.exec("$(xmake) --version")

About on_clean problem, I have fixed it on previous commit. You can test it again after updating xmake.

About the third problem, the default directoy of the before_build script is the project root directory ($(projectdir)). You can use $(scriptdir) variable to enter the current script(xmake.lua) directory

before_build(function (target)
        os.cd("$(scriptdir)/shared")
        os.exec("UpdateGenVersion.bat")
        os.cd("-")
    end)

Or

before_build(function (target)
        os.exec("$(scriptdir)/shared/UpdateGenVersion.bat")
end)

And it's no problem about add_subdirs, it's not directly related to change directory.

add_subdirs(path.join("winpty","src")). It works normally.

@ghost
Copy link
Author

ghost commented Apr 16, 2017

Great!!!:+1::smile:

@ghost ghost closed this as completed Apr 16, 2017
@waruqi
Copy link
Member

waruqi commented May 12, 2017

I improved phony target to support the following case:

set_kind("binary")

target("test1")
    add_files("*.c")

target("test2")
    add_files("*.c")

target("alltests")
    set_kind("phony")    <---   If you do not set kind, this target kind is `binary` instead of `phony`
    add_deps("test1", "test2")

This issue was closed.
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

1 participant