diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2025-01-05 21:40:39 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2025-01-05 21:40:39 -0300 |
commit | f5c5db5a7378d97a6f261ffaa10c84e2005bfc4e (patch) | |
tree | c2f0cb86890fb90a85a7f091746543c7ff14ac5e /docs/recommended_practices_for_makefiles.md | |
parent | 7fe5532c695711d23fd9d7e13a9e6235478171b6 (diff) | |
download | luarocks-f5c5db5a7378d97a6f261ffaa10c84e2005bfc4e.tar.gz luarocks-f5c5db5a7378d97a6f261ffaa10c84e2005bfc4e.tar.bz2 luarocks-f5c5db5a7378d97a6f261ffaa10c84e2005bfc4e.zip |
docs: import Wiki docs into the main repo
Diffstat (limited to 'docs/recommended_practices_for_makefiles.md')
-rw-r--r-- | docs/recommended_practices_for_makefiles.md | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/docs/recommended_practices_for_makefiles.md b/docs/recommended_practices_for_makefiles.md new file mode 100644 index 00000000..1391ec3b --- /dev/null +++ b/docs/recommended_practices_for_makefiles.md | |||
@@ -0,0 +1,176 @@ | |||
1 | # Recommended practices for Makefiles | ||
2 | |||
3 | When authoring a Lua module, especially those containing C code, developers | ||
4 | are always faced with some build and deployment issues: where to find | ||
5 | libraries, where to install modules, which flags to pass when building, and so | ||
6 | on. Looking at the existing modules available in the web, it is clear to see | ||
7 | that there are no _de facto_ standards in Makefiles for Lua modules, and that | ||
8 | many of them are incomplete copies of one another, and many of them share the | ||
9 | same deficiencies. Here is a list of some of those issues we found during the | ||
10 | development of LuaRocks, and how to avoid them. Following the recommendations | ||
11 | below will improve the portability of your Makefiles and make things easier | ||
12 | for writing rocks, other packagers such as Linux distributions, and your | ||
13 | users. | ||
14 | |||
15 | # Do not ask users to edit files "by hand" | ||
16 | |||
17 | Asking users to hand-edit things is error-prone. Even though systems like | ||
18 | LuaRocks support applying patches -- which is the automated equivalent of | ||
19 | hand-tweaking files -- this adds maintenance burden, as patches have to be | ||
20 | updated on each release if any change happens in the file. Always use | ||
21 | variables, so that they can be overridden by automated processes. Hand-tweaks | ||
22 | in the code can be avoided by propagating C defines from Makefiles as well. | ||
23 | |||
24 | Don't: | ||
25 | ``` | ||
26 | // Edit this to suit your installation | ||
27 | const char* bla_dir = "/usr/local/share/bla"; | ||
28 | ``` | ||
29 | |||
30 | Do: | ||
31 | |||
32 | ``` | ||
33 | BLA_DIR=/usr/local/share/bla | ||
34 | # ... | ||
35 | bla.o: | ||
36 | $(CC) -c bla.c -DBLA_DIR=\"$(BLA_DIR)\" | ||
37 | ``` | ||
38 | |||
39 | ## Do not hardcode any paths | ||
40 | |||
41 | This is a corollary to the above recommendation, really. Whenever you are | ||
42 | passing any paths, don't write them directly in Makefile rules. Always factor | ||
43 | them out into variables. | ||
44 | |||
45 | # Provide a nice "install" rule | ||
46 | |||
47 | A large number of Makefiles for Lua modules ship without an "install" rule, | ||
48 | probably due to the long-standing lack of standard install locations for | ||
49 | modules that plagued the Lua world in the past. Nowadays, however, the | ||
50 | convention of using .../lib/lua/5.1/ for C modules and .../share/lua/5.1/ for | ||
51 | Lua modules (relative to the prefix where Lua is installed) is well | ||
52 | established, and there is no reason not to use it. To make things even easier | ||
53 | for your users, you can also factor out the common Lua prefix. /usr/local is a | ||
54 | good default, since it is also the default for the vanilla Lua tarball. | ||
55 | |||
56 | Don't: | ||
57 | |||
58 | ``` | ||
59 | # no make install rule! | ||
60 | ``` | ||
61 | |||
62 | Do: | ||
63 | |||
64 | ``` | ||
65 | LUA_DIR=/usr/local | ||
66 | LUA_LIBDIR=$(LUA_DIR)/lib/lua/5.1 | ||
67 | LUA_SHAREDIR=$(LUA_DIR)/share/lua/5.1 | ||
68 | |||
69 | # ... | ||
70 | |||
71 | install: | ||
72 | mkdir -p $(LUA_LIBDIR)/bla | ||
73 | cp bla/core.so $(LUA_LIBDIR)/bla | ||
74 | mkdir -p $(LUA_SHAREDIR)/bla | ||
75 | cp bla.lua $(LUA_SHAREDIR) | ||
76 | cp bla/extras.lua $(LUA_SHAREDIR)/bla | ||
77 | ``` | ||
78 | |||
79 | Some packagers recommend prepending an empty `$(DESTDIR)` variable to all target | ||
80 | paths in your install rule, but that's not strictly necessary if your paths | ||
81 | are all set into variables, which can be redefined for the "make install" run, | ||
82 | like in the example above. | ||
83 | |||
84 | # Do not assume libraries are in the system path | ||
85 | |||
86 | If your program uses LibPNG, adding "-lpng" to your Makefile is not enough. | ||
87 | Your users may have the LibPNG library somewhere else, so let them specify the | ||
88 | locations of both the libraries and headers. The default values you pick are | ||
89 | not really important, as long as they're overridable by the user, but | ||
90 | /usr/local is always a good choice on Unix systems as this is the first | ||
91 | typical "non-system path" that users may want to use. | ||
92 | |||
93 | Of course, use one library per value; don't assume all third-party libraries | ||
94 | can be found in the same place. | ||
95 | |||
96 | Don't: | ||
97 | |||
98 | ``` | ||
99 | gcc -o bla bla.c -lpng | ||
100 | ``` | ||
101 | |||
102 | Do: | ||
103 | |||
104 | ``` | ||
105 | LIBPNG_DIR=/usr/local | ||
106 | LIBPNG_INCDIR=$(LIBPNG_DIR)/include | ||
107 | LIBPNG_LIBDIR=$(LIBPNG_DIR)/lib | ||
108 | |||
109 | # ... | ||
110 | $(CC) -o bla bla.c -lpng -L$(LIBPNG_LIBDIR) -I$(LIBPNG_INCDIR) | ||
111 | ``` | ||
112 | |||
113 | # Avoid compiler assumptions | ||
114 | |||
115 | Even if your code only compiles in GCC and is Unix-only, there are still build portability issues to look out for. The main ones are: | ||
116 | |||
117 | * **Not all GCCs link libraries with the same flags** - for instance, linking | ||
118 | shared libraries is done with "-shared" on Linux and "-bundle -undefined | ||
119 | dynamic_lookup -all_load" on Mac OSX. Factoring the flags in a variable is | ||
120 | a nice gesture for users of other operating systems. (LuaRocks can take | ||
121 | care of the detection part and set the flag appropriately, but packages of | ||
122 | other systems will benefit as well.) | ||
123 | |||
124 | * **The compiler is not always called "gcc"** - this one is for the people in | ||
125 | the embedded world; in their toolchains, their cross-compilers often have | ||
126 | names like "arm-nofpu-gcc". $(CC) is a standard variable in make. Just use | ||
127 | that instead of "gcc" and you're good to go. | ||
128 | |||
129 | Don't: | ||
130 | |||
131 | ``` | ||
132 | bla.so: | ||
133 | gcc -o bla.so -shared bla.o | ||
134 | ``` | ||
135 | |||
136 | Do: | ||
137 | |||
138 | ``` | ||
139 | LIBFLAG=-shared | ||
140 | # ... | ||
141 | bla.so: | ||
142 | $(CC) -o bla.so $(LIBFLAG) bla.o | ||
143 | ``` | ||
144 | |||
145 | # Do not "overload" variables | ||
146 | |||
147 | As mentioned in the topic about third-party libraries above, don't reuse variables just because two conceptually different locations happen to point to the same place. For example, do not assume that the directory you're using to _find_ libraries and the directory you're _installing_ libraries to is the same. That little economy will only cause confusion and trouble to your users. | ||
148 | |||
149 | Don't: | ||
150 | |||
151 | ``` | ||
152 | LIBDIR=/usr/local/lib | ||
153 | INCDIR=/usr/local/include | ||
154 | # ... | ||
155 | bla.so: | ||
156 | $(CC) $(LIBFLAG) -o bla.so -lpng -L$(LIBDIR) -I$(INCDIR) | ||
157 | install: | ||
158 | mkdir -p $(LIBDIR) | ||
159 | cp bla.so $(LIBDIR)/lua/5.1 | ||
160 | ``` | ||
161 | |||
162 | Do: | ||
163 | |||
164 | ``` | ||
165 | LIBPNG_DIR=/usr/local | ||
166 | LIBPNG_LIBDIR=$(LIBPNG_DIR)/lib | ||
167 | LIBPNG_INCDIR=$(LIBPNG_DIR)/include | ||
168 | LUA_DIR=/usr/local | ||
169 | LUA_LIBDIR=$(LUA_DIR)/lib/lua/5.1 | ||
170 | # ... | ||
171 | bla.so: | ||
172 | $(CC) $(LIBFLAG) -o bla.so -lpng -L$(LIBPNG_LIBDIR) -I$(LIBPNG_INCDIR) | ||
173 | install: | ||
174 | mkdir -p $(LUA_LIBDIR) | ||
175 | cp bla.so $(LUA_LIBDIR) | ||
176 | ``` | ||