Skip to content

Instantly share code, notes, and snippets.

@MikuAuahDark
Created March 12, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikuAuahDark/2926368c73b24aead1eb to your computer and use it in GitHub Desktop.
Save MikuAuahDark/2926368c73b24aead1eb to your computer and use it in GitHub Desktop.
Makefile-compatible Lua script/Lua-compatible Makefile/Lua script in Makefile
__unused = #string -- This is a Makefile which is also executable by Lua
__unused = #string --[[
#Your Makefile script starts here
default:
@echo "Hello!"
#Your Makefile script ends here
ifeq (0,1)
__unused = #string ]]--
-- Your lua script starts here
print("Hello!")
-- Your lua script ends here
__unused = #string --[[
endif
__unused = #string ]]--
@MikuAuahDark
Copy link
Author

Explanation

First line, __unused = #string -- This is a Makefile which is also executable by Lua is both valid in Lua and Makefile. It will set global variable __unused to 0 (because #string is 0) in Lua, while it will set __unused to #string -- This is a Makefile which is also executable by Lua in Makefile

Second line, __unused variable will be set to #string --[[ in Makefile. While in Lua, it will set __unused global variable AGAIN to 0 and start block of comment (--[[). After that line, all makefile script will be valid, as long as it doesn't have character ]] in the Makefile string.

Line 8 will absolutely false, so Makefile won't see it and we can put anything in it (source) as long as it is not one of Makefile keywords and/or symbols.

Line 9 will actually closes the comment block in Lua (]]--) and below that line, you can write your Lua script. Just make sure that it doesn't contain any of Makefile keywords and/or symbols or Makefile will complain about it.

When your script ends, line 14 will open block of comment again (--[[) and line 15, closes the ifeq statement for Makefile in line 8. To prevent Lua complaining about unfinished long comment, __unused will be set again to #string ]]-- and closes the Lua comment block because ]]--.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment