{"id":24,"date":"2011-10-24T16:40:05","date_gmt":"2011-10-24T16:40:05","guid":{"rendered":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/?p=24"},"modified":"2022-05-24T16:41:06","modified_gmt":"2022-05-24T16:41:06","slug":"playing-with-the-tools-build-system","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/2011\/10\/24\/playing-with-the-tools-build-system\/","title":{"rendered":"Playing with the tools build system"},"content":{"rendered":"<p>The <a href=\"https:\/\/github.com\/residualvm\/residualvm-tools\">residualvm-tools<\/a> repository was born on April, 7th this year. From the very first moment I felt its build system was broken, mainly for these three reasons:<\/p>\n<ol>\n<li>to build a specific tool you must run \u201cmake tools\/foo\u201d. The repo is called residualvm-<strong>tools<\/strong>, there should be no need to specify \u201ctools\u201d in the make call too;<\/li>\n<li>there isn\u2019t a single line of Makefile code shared between the different tool targets;<\/li>\n<li>each submodule of tools\/, like tools\/lua\/module.mk, needs to be added in Makefile.common. I\u2019d like a way to set it up into tools\/module.mk, with a parent-child relationship.<\/li>\n<\/ol>\n<p>So some days ago I decided to try to improve it a bit, even though I never wrote a single line of a Makefile.<\/p>\n<p>My first try was to work on rules.mk: I modified a bit the code that created a target from the value of TOOL_EXECUTABLE and of some other variables. After some trial and error I came up with a piece of code that allowed me to put in tools\/module.mk, for each tool, something like this:<\/p>\n<pre>TOOL_EXECUTABLE := unlab\r\nMODULE_OBJS := unlab.o\r\ninclude $(srcdir)\/rules.mk\r\n\r\nTOOL_EXECUTABLE := vima\r\nMODULE_OBJS := vima.o\r\ninclude $(srcdir)\/rules.mk\r\n\r\n....<\/pre>\n<p>Quite ugly, but still better than before.<\/p>\n<p>Then I started working on 3. Long story short, I couldn\u2019t come up with an acceptable solution.<\/p>\n<p>Then an heretic thought passed through my mind: CMake!<br \/>\nI really like the <code>add_executable<\/code> and <code>add_subdirectory<\/code> functions of CMake, but I know that porting to CMake would require a big effort, since it doesn\u2019t work out of the box on all the platforms <code>configure<\/code> currently supports.<br \/>\nSo I started thinking: <code>configure<\/code> works perfectly, I just need a way to confortably generate a bit of Makefile code. configure is an sh script, why not just use sh?<\/p>\n<p>So I wrote a script, <a href=\"https:\/\/github.com\/giucam\/residual-tools\/blob\/63568230e1033882a91b2ec85928d983f91e52d4\/makegen\">makegen<\/a>, with the functions <code>add_executable<\/code> and <code>add_subdirectory<\/code>.<\/p>\n<pre>add_subdirectory() {\r\n    MODULE=\"$MODULE\/$1\"\r\n    . $srcdir\/$MODULE\/module.mg\r\n    MODULE=\"${MODULE%\/$1}\"\r\n}\r\n\r\nadd_executable() {\r\n    echo -n \"$1_SOURCES := \" &gt;&gt; $builddir\/Makefile.modules\r\n    for f in $2; do\r\n        echo -n \"$MODULE\/$f \" &gt;&gt; $builddir\/Makefile.modules\r\n    done\r\n    echo &gt;&gt; $builddir\/Makefile.modules\r\n\r\n    echo -e \\\r\n\"build: $1\r\n$1_DIRS := \\$(sort \\$(dir \\$($1_SOURCES)))\r\n$1_dirs:\r\n    \\$(QUIET)\\$(MKDIR) \\$($1_DIRS)\r\n$1: $1_dirs \\$($1_SOURCES)\r\n    \\$(QUIET)\\$(MKDIR) \\$(DEPDIR)\r\n    \\$(QUIET_CXX)\\$(CXX) \\$(LDFLAGS) $3 \\$(DEFINES) -I. -I\\$(srcdir) \\\r\n    \\$(filter %.cpp %.o, $+) -o \\$@\\$(EXEEXT)\r\nEXECUTABLES += $1\\$(EXEEXT)\r\nclean: clean\/$1\r\nclean\/$1:\r\n    \\$(QUIET)\\$(RM) \\$($1_SOURCES) $1\r\n    \\$(RMDIR) \\$($1_DIRS) \\n\" &gt;&gt; $builddir\/Makefile.modules\r\n}<\/pre>\n<p>This script is called from configure and it simply generates a file <code>Makefile.modules<\/code> with the targets for the tools.<\/p>\n<p>Now, adding a new tool is simply a matter of adding in tools\/module.mg<\/p>\n<pre>add_executable toolname \"toolname.o foo.o bar.o\"<\/pre>\n<p>You\u2019ll then be able to call <code>make toolname<\/code>.<\/p>\n<p>This is not finished and still experimental, and I can\u2019t know if it will ever be actually used, but I would like to hear some thoughts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The residualvm-tools repository was born on April, 7th this year. From the very first moment I felt its build system was broken, mainly for these three reasons: to build a specific tool you must run \u201cmake tools\/foo\u201d. The repo is called residualvm-tools, there should be no need to specify \u201ctools\u201d in the make call too; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/posts\/24\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/lostsoulsalliance\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}