~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to bam/src/base.bam

  • Committer: Bazaar Package Importer
  • Author(s): Gonéri Le Bouder
  • Date: 2009-04-12 02:32:37 UTC
  • mfrom: (3.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090412023237-ufmf1xn0rkjmx6f3
Tags: 0.5.1-2
* Fix the ouput of teeworlds-server --help with /bin/sh ->
  /bin/bash (Closes: #511600)
* Standard version 3.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function rfind(s, what)
2
 
        local p = 1
3
 
        while true do
4
 
                n = string.find(s, what, p)
5
 
                if n then
6
 
                        p = n+1
7
 
                else
8
 
                        break
9
 
                end
10
 
        end
11
 
        return p-1
12
 
end
13
 
 
14
 
function file_ext(s)
15
 
        for ext in string.gfind(s, "%.%a+$") do
16
 
                return string.sub(ext, 2)
17
 
        end
18
 
        return ""
19
 
end
20
 
 
21
 
function PathBase(s)
22
 
        local e = file_ext(s)
23
 
        if e == "" then
24
 
                return s
25
 
        end
26
 
        
27
 
        return string.sub(s,1,string.len(s)-string.len(file_ext(s))-1)
28
 
end
29
 
 
30
 
function file_path(s)
31
 
        n = rfind(s, "/")
32
 
        if n > 0 then
33
 
                return string.sub(s,1,n)
34
 
        end
35
 
        return ""
36
 
end
37
 
 
38
 
function str_replace(s, pattern, what)
39
 
        return string.gsub(s, pattern, function(v) return what end)
40
 
end
41
 
 
42
 
function flattern_table(collection, t)
43
 
        for i,v in ipairs(t) do
44
 
                if type(v) == type({}) then
45
 
                        flattern_table(collection, v)
46
 
                elseif type(v) == type("") then
47
 
                        table.insert(collection, v)
48
 
                end             
49
 
        end
50
 
end
51
 
 
52
 
function collect_input(a)
53
 
        local inputs = {}
54
 
        flattern_table(inputs, a)
55
 
        return inputs
56
 
end
57
 
 
58
 
 
59
 
function tbl_to_str(tbl, prefix, postfix)
60
 
        local s = ""
61
 
        for index,value in ipairs(tbl) do
62
 
                if type(value) == type("") then
63
 
                        s = s .. prefix .. value .. postfix
64
 
                end
65
 
        end
66
 
        return s
67
 
end
68
 
 
69
 
Path = bam_path_fix
70
 
 
71
 
function PathFilename(path)
72
 
        n = rfind(path, "/")
73
 
        if n > 0 then
74
 
                return string.sub(path,n+1)
75
 
        end
76
 
        return ""       
77
 
end
78
 
 
79
 
function NewTable()
80
 
        local t = {}
81
 
        t.add = function(self, ...)
82
 
                for i,what in ipairs(arg) do
83
 
                        table.insert(self, what)
84
 
                end
85
 
        end
86
 
        return t
87
 
end
88
 
 
89
 
function NewPathTable()
90
 
        local t = {}
91
 
        t.add = function(self, value)
92
 
                table.insert(self, Path(value))
93
 
        end
94
 
        return t
95
 
end
96
 
 
97
 
function NewAntiPathTable()
98
 
        local t = {}
99
 
        t.add = function(self, value)
100
 
                print(value)
101
 
                table.insert(self, PathFilename(value))
102
 
        end
103
 
        return t
104
 
end
105
 
 
106
 
 
107
 
function default_intermediate_output_func(dir, input, extension)
108
 
        if not (dir == "") then
109
 
                return Path(dir .. "/" .. PathBase(PathFilename(input)) .. extension)
110
 
        end
111
 
        return PathBase(input) .. extension
112
 
end
113
 
 
114
 
function default_link_output_func(dir, input, extension)
115
 
        if not (dir == "") then
116
 
                return Path(dir .. "/" .. PathFilename(input) .. extension)
117
 
        end
118
 
        return Path(input) .. extension
119
 
end
120
 
 
121
 
function table.copy(self, ud_copy_fn)
122
 
    ud_copy_fn = ud_copy_fn or function ( ud ) return ud end
123
 
    
124
 
    local new_table = {}
125
 
    for key, value in self do
126
 
        local new_key
127
 
        if(type(key) == 'table') then
128
 
            new_key = table.copy(key, ud_copy_fn)
129
 
        elseif (type(key) == 'userdata') then
130
 
            new_key = ud_copy_fn(key)
131
 
        else
132
 
            new_key = key
133
 
        end
134
 
 
135
 
        local new_value
136
 
        if(type( value ) == 'table' ) then
137
 
            new_value = table.copy(value, ud_copy_fn)
138
 
        elseif(type(value) == 'userdata') then
139
 
            new_value = ud_copy_fn(value)
140
 
        else
141
 
            new_value = value
142
 
        end
143
 
 
144
 
        new_table[new_key] = new_value
145
 
    end
146
 
 
147
 
     return new_table
148
 
end
149
 
 
150
 
function table.lock(t)
151
 
        mt = getmetatable(t)
152
 
        if not mt then mt = {} end
153
 
        mt.__newindex = function(t, key, value)
154
 
                error("trying to create key '" .. key .. "' on a locked table")
155
 
        end
156
 
        setmetatable(t,mt)
157
 
end
158
 
 
159
 
function NewSettings()
160
 
        local s = {}
161
 
        
162
 
        s.copy = table.copy
163
 
        
164
 
        s.debug = 1
165
 
        s.objdir = ""
166
 
        s.libdir = ""
167
 
        s.exedir = ""
168
 
        s.config_name = ""
169
 
        s.config_ext = ""
170
 
        
171
 
        s.cc = {}
172
 
        s.cc.c_compiler = "gcc"
173
 
        s.cc.cxx_compiler = "g++"
174
 
        s.cc.flags = ""
175
 
        s.cc.includes = NewPathTable()
176
 
        s.cc.defines = NewTable()
177
 
        s.cc.frameworks = NewTable()
178
 
        s.cc.output = default_intermediate_output_func
179
 
        s.cc.optimize = 0
180
 
        s.cc._propagate = function(cc, s) 
181
 
                cc.debug = s.debug
182
 
        end
183
 
        s.cc._propagate(s.cc, s)
184
 
        
185
 
        s.linker = {}
186
 
        s.linker.linker = "g++"
187
 
        s.linker.inputflags = ""
188
 
        s.linker.flags = ""
189
 
        s.linker.libs = NewTable()
190
 
        s.linker.frameworks = NewTable()
191
 
        s.linker.frameworkpath = NewPathTable()
192
 
        s.linker.libpath = NewPathTable()
193
 
        s.linker.extrafiles = NewPathTable()
194
 
        s.linker.output = default_intermediate_output_func
195
 
        s.linker._propagate = function(linker, s) 
196
 
                linker.debug = s.debug
197
 
        end
198
 
        s.linker._propagate(s.linker, s)
199
 
        
200
 
        s.lib = {}
201
 
        s.lib.flags = ""
202
 
        s.lib.output = default_intermediate_output_func
203
 
        s.lib._propagate = function(lib, s) 
204
 
                lib.debug = s.debug
205
 
        end
206
 
        s.lib._propagate(s.lib, s)
207
 
 
208
 
        s.dll = {}
209
 
        s.dll.linker = "g++"
210
 
        s.dll.inputflags = ""
211
 
        s.dll.flags = ""
212
 
        s.dll.libs = NewTable()
213
 
        s.dll.frameworks = NewTable()
214
 
        s.dll.frameworkpath = NewPathTable()
215
 
        s.dll.libpath = NewPathTable()
216
 
        s.dll.extrafiles = NewPathTable()
217
 
        s.dll.output = default_intermediate_output_func
218
 
        s.dll._propagate = function(dll, s) 
219
 
                dll.debug = s.debug
220
 
        end
221
 
        s.dll._propagate(s.dll, s)
222
 
 
223
 
        if family == "windows" then
224
 
                s.cc.extension = ".obj"
225
 
                s.linker.extension = ".exe"
226
 
                s.lib.extension = ".lib"
227
 
                s.dll.extension = ".dll"
228
 
        else
229
 
                s.cc.extension = ".o"
230
 
                s.linker.extension = ""
231
 
                s.lib.extension = ".a"
232
 
                if platform == "macosx" then
233
 
                        s.dll.extension = ".dylib"
234
 
                else
235
 
                        s.dll.extension = ".so" 
236
 
                end
237
 
        end
238
 
 
239
 
        -- lock the tables
240
 
        table.lock(s)   
241
 
        table.lock(s.cc)
242
 
        table.lock(s.linker)
243
 
        table.lock(s.lib)
244
 
        table.lock(s.dll)
245
 
 
246
 
        return s
247
 
end
248
 
 
249
 
function Collect(...)
250
 
        local f = {}
251
 
        for i,what in ipairs(arg) do
252
 
                directory = file_path(what)
253
 
                pattern = string.sub(what, string.len(directory)+1)
254
 
                pattern = str_replace(pattern, "%.", "%.")
255
 
                pattern = str_replace(pattern, "%*", ".*")
256
 
                pattern = pattern .. "$"
257
 
                k = CollectAdv(bam_path_fix(directory), pattern)
258
 
                for index,value in k do
259
 
                        table.insert(f,value)
260
 
                end
261
 
        end
262
 
        return f
263
 
end
264
 
 
265
 
function CollectAdv(directory, pattern)
266
 
        local d = support.listdir(directory)
267
 
        local f = {}
268
 
        for i,v in d do
269
 
                if string.find(v,pattern) then
270
 
                        table.insert(f, v)
271
 
                end
272
 
        end
273
 
        return f
274
 
end
275
 
 
276
 
function copy(output, input)
277
 
        print("copy " .. PathFilename(output))
278
 
    local copy_command
279
 
    
280
 
    if family == "windows" then
281
 
        copy_command = "copy"
282
 
        input = str_replace(input, "/", "\\")
283
 
        output = str_replace(output, "/", "\\")
284
 
    else
285
 
        copy_command = "cp"
286
 
    end
287
 
 
288
 
    os.execute(copy_command .. " " .. input .. " " .. output)
289
 
        return 0
290
 
end
291
 
 
292
 
function Copy(outputdir, ...)
293
 
        local inputs = collect_input(arg)
294
 
        local outputs = {}
295
 
        
296
 
        -- compile all the files
297
 
        for index, inname in inputs do
298
 
                output = Path(outputdir .. "/" .. PathFilename(inname))
299
 
                input = Path(inname)
300
 
                bam_add_job("copy", output, input)
301
 
                bam_add_dependency(output, input)
302
 
                table.insert(outputs, output)
303
 
        end
304
 
        
305
 
        return outputs
306
 
end
307
 
 
308
 
function Compile(settings, ...)
309
 
        local inputs = collect_input(arg)
310
 
        local outputs = {}
311
 
        
312
 
        settings.cc:_propagate(settings);
313
 
        
314
 
        -- compile all the files
315
 
        for index, inname in inputs do
316
 
 
317
 
                -- fetch correct compiler
318
 
                ext = file_ext(inname)
319
 
                compiler = ""
320
 
                if ext == "cpp" then compiler = "cxx" end
321
 
                if ext == "c++" then compiler = "cxx" end
322
 
                if ext == "cxx" then compiler = "cxx" end
323
 
                if ext == "cc" then compiler = "cxx" end
324
 
                if ext == "c" then compiler = "c" end
325
 
                if ext == "S" then compiler = "c" end
326
 
                if ext == "m" then compiler = "c" end
327
 
 
328
 
                if compiler == "" then
329
 
                        error("don't know how to compile a file with the ending '"..ext.."'")
330
 
                end
331
 
 
332
 
                outname = settings.cc.output(settings.objdir, inname, settings.config_ext ..settings.cc.extension)
333
 
                
334
 
                -- add job and run dependencies
335
 
                bam_add_job("compile_"..compiler, outname, inname, settings.cc)
336
 
                bam_add_dependency(outname, inname)
337
 
                
338
 
                bam_dependency_cpp(inname, settings.cc.includes)
339
 
                
340
 
                table.insert(outputs, outname)
341
 
        end
342
 
 
343
 
        -- return the output
344
 
        return outputs  
345
 
end
346
 
 
347
 
function Link(settings, output, ...)
348
 
        local inputs = collect_input(arg)
349
 
        
350
 
        settings.linker:_propagate(settings);
351
 
 
352
 
        output = settings.linker.output(settings.exedir, Path(output), settings.config_ext .. settings.linker.extension)
353
 
        bam_add_job("link", output, inputs, settings.linker)
354
 
 
355
 
        -- all the files
356
 
        for index, inname in inputs do
357
 
                bam_add_dependency(output, inname)
358
 
        end
359
 
        
360
 
        for index, inname in ipairs(settings.linker.extrafiles) do
361
 
                bam_add_dependency(output, inname)
362
 
        end
363
 
 
364
 
        return output
365
 
end
366
 
 
367
 
function StaticLibrary(settings, output, ...)
368
 
        local inputs = collect_input(arg)
369
 
 
370
 
        settings.lib:_propagate(settings);
371
 
 
372
 
        output = settings.lib.output(settings.libdir, Path(output), settings.config_ext ..settings.lib.extension)
373
 
        bam_add_job("lib", output, inputs, settings.lib)
374
 
 
375
 
        for index, inname in inputs do
376
 
                bam_add_dependency(output, inname)
377
 
        end
378
 
 
379
 
        return output
380
 
end
381
 
 
382
 
function SharedLibrary(settings, output, ...)
383
 
        local inputs = collect_input(arg)
384
 
 
385
 
        settings.dll:_propagate(settings);
386
 
 
387
 
        output = settings.dll.output(settings.libdir, Path(output), settings.config_ext .. settings.dll.extension)
388
 
        bam_add_job("dll", output, inputs, settings.dll)
389
 
 
390
 
        for index, inname in inputs do
391
 
                bam_add_dependency(output, inname)
392
 
        end
393
 
 
394
 
        return output
395
 
end
396
 
 
397
 
function Import(filename, options)
398
 
        -- filename = Path(filename)
399
 
 
400
 
        -- clone the bam base stuff
401
 
        local module = {}
402
 
        for i,v in _bam_clone do
403
 
                module[i] = v
404
 
        end
405
 
        
406
 
        local x = loadfile(bam_path_fix(filename))
407
 
        if not x then
408
 
                error("Import: Could not open '" .. filename .. "'")
409
 
        end
410
 
        
411
 
        setfenv(x, module)
412
 
 
413
 
        -- set magic values
414
 
        local mypath = _bam_path
415
 
        module._bam_path = file_path(bam_path_fix(filename))
416
 
        _bam_path = module._bam_path
417
 
 
418
 
        -- pass the options
419
 
        module.options = options
420
 
        
421
 
        -- call the module
422
 
        x()
423
 
        
424
 
        -- restore magic values
425
 
        _bam_path = mypath
426
 
        return module
427
 
end
428
 
 
429
 
Target = bam_add_target
430
 
DefaultTarget = bam_default_target
431
 
 
432
 
function pseudo(output)
433
 
end
434
 
 
435
 
function PseudoTarget(name, ...)
436
 
        local inputs = collect_input(arg)
437
 
        local name = bam_path_fix(name)
438
 
        bam_add_job("pseudo", name)
439
 
 
440
 
        -- all the files
441
 
        for index, inname in inputs do
442
 
                bam_add_dependency(name, inname)
443
 
        end
444
 
 
445
 
        return name
446
 
end
447
 
 
448
 
function Debug_DumpArguments()
449
 
        print("-- Dumping arguments --")
450
 
        for index, value in _cn_options do
451
 
                print(index, value)
452
 
                for i,v in value do
453
 
                        print("\t",i,v)
454
 
                end
455
 
        end
456
 
        print("--")
457
 
end
458
 
 
459
 
----- gcc/g++ compiler ------
460
 
function compile_c_cxx_gcc(type, output, input, settings)
461
 
        local d = tbl_to_str(settings.defines, '-D', ' ')
462
 
        local i = tbl_to_str(settings.includes, '-I', ' ')
463
 
        local i = i .. tbl_to_str(settings.frameworks, '-framework ', ' ')
464
 
        local f = settings.flags .. ' '
465
 
        if settings.debug > 0 then f = f .. "-g " end
466
 
        if settings.optimize > 0 then f = f .. "-O2 " end
467
 
        local e = type .. ' ' .. f ..'-c ' .. input .. ' -o ' .. output .. ' ' .. d .. i
468
 
        if verbose > 0 then print(e) end
469
 
        return os.execute(e)
470
 
end
471
 
 
472
 
function compile_cxx_gcc(output,input,settings)
473
 
        print("c++ " .. PathFilename(input))
474
 
        return compile_c_cxx_gcc(settings.cxx_compiler,output,input,settings)
475
 
end
476
 
 
477
 
function compile_c_gcc(output,input,settings)
478
 
        print("c " .. PathFilename(input))
479
 
        return compile_c_cxx_gcc(settings.c_compiler,output,input,settings)
480
 
end
481
 
 
482
 
 
483
 
function lib_ar(output, inputs, settings)
484
 
        print("lib " .. PathFilename(output))
485
 
        local e = "ar qc " .. output
486
 
        local e = e .. " " .. tbl_to_str(inputs, '', ' ') .. settings.flags
487
 
        if verbose > 0 then print(e) end
488
 
        os.execute("rm -f " .. output)
489
 
        return os.execute(e)
490
 
end
491
 
 
492
 
function dll_gcc(output, inputs, settings)
493
 
        print("dll " .. PathFilename(output))
494
 
        local shared_flags = ""
495
 
 
496
 
        if platform == "macosx" then
497
 
                shared_flags = " -dynamiclib"
498
 
        else
499
 
                shared_flags = " -shared"
500
 
        end
501
 
 
502
 
        local e = settings.linker .. shared_flags .. " -o " .. output
503
 
        local e = e .. " " .. settings.inputflags .. " " .. tbl_to_str(inputs, '', ' ') 
504
 
        local e = e .. tbl_to_str(settings.extrafiles, '', ' ')
505
 
        local e = e .. tbl_to_str(settings.libpath, '-L', ' ')
506
 
        local e = e .. tbl_to_str(settings.libs, '-l', ' ')
507
 
        local e = e .. tbl_to_str(settings.frameworkpath, '-F', ' ')
508
 
        local e = e .. tbl_to_str(settings.frameworks, '-framework ', ' ')
509
 
        local e = e .. settings.flags
510
 
        if verbose > 0 then print(e) end
511
 
        return os.execute(e)
512
 
end
513
 
 
514
 
function link_gcc(output, inputs, settings)
515
 
        print("link " .. PathFilename(output))
516
 
        local e = settings.linker .. " -o " .. output
517
 
        local e = e .. " " .. settings.inputflags .. " " .. tbl_to_str(inputs, '', ' ') 
518
 
        local e = e .. tbl_to_str(settings.extrafiles, '', ' ')
519
 
        local e = e .. tbl_to_str(settings.libpath, '-L', ' ')
520
 
        local e = e .. tbl_to_str(settings.libs, '-l', ' ')
521
 
        local e = e .. tbl_to_str(settings.frameworkpath, '-F', ' ')
522
 
        local e = e .. tbl_to_str(settings.frameworks, '-framework ', ' ')
523
 
        local e = e .. settings.flags
524
 
        if verbose > 0 then print(e) end
525
 
        return os.execute(e)
526
 
end
527
 
 
528
 
----- cl compiler ------
529
 
function compile_c_cxx_cl(output, input, settings)
530
 
        local defs = tbl_to_str(settings.defines, "-D", " ") .. " "
531
 
        local incs = tbl_to_str(settings.includes, "-I", " ")
532
 
        local flags = settings.flags
533
 
        if platform =="win32" then
534
 
                flags = flags .. " /D \"WIN32\" "
535
 
        else
536
 
                flags = flags .. " /D \"WIN64\" "
537
 
        end
538
 
        if settings.debug > 0 then flags = flags .. "/Od /MTd /Zi /D \"_DEBUG\" " end
539
 
        if settings.optimize > 0 then flags = flags .. "/Ox /Ot /MT /D \"NDEBUG\" " end
540
 
        local exec = "cl /nologo /D_CRT_SECURE_NO_DEPRECATE /c " .. flags .. input .. " " .. incs .. defs .. " /Fo" .. output
541
 
        if verbose > 0 then print(exec) end
542
 
        return os.execute(exec)
543
 
end
544
 
 
545
 
function compile_cxx_cl(output,input,settings)
546
 
        -- support.simpleprint("c++ ")
547
 
        return compile_c_cxx_cl(output,input,settings)
548
 
end
549
 
 
550
 
function compile_c_cl(output,input,settings)
551
 
        -- support.simpleprint("c ")
552
 
        return compile_c_cxx_cl(output,input,settings)
553
 
end
554
 
 
555
 
function lib_cl(output, inputs, settings)
556
 
        -- support.simpleprint("lib ")
557
 
        local input =  tbl_to_str(inputs, "", " ")
558
 
        local exec = "lib /nologo " .. settings.flags .. " /OUT:" .. output .. " " .. input
559
 
        if verbose > 0 then print(exec) end
560
 
        return os.execute(exec)
561
 
end
562
 
 
563
 
function dll_cl(output, inputs, settings)
564
 
        -- support.simpleprint("lib ")
565
 
        local input =  tbl_to_str(inputs, "", " ")
566
 
        local flags = settings.flags .. " "
567
 
        local libs  = tbl_to_str(settings.libs, "", " ")
568
 
        local libpaths = tbl_to_str(settings.libpath, "/libpath:\"", "\" ")
569
 
        local exec = "link /nologo /DLL" .. flags .. libpaths .. libs .. " /OUT:" .. output .. " " .. input
570
 
        if verbose > 0 then print(exec) end
571
 
        return os.execute(exec)
572
 
end
573
 
 
574
 
function link_cl(output, inputs, settings)
575
 
        support.simpleprint("link ")
576
 
        local input =  tbl_to_str(inputs, "", " ")
577
 
        local flags = settings.flags .. " "
578
 
        local libs  = tbl_to_str(settings.libs, "", " ")
579
 
        local libpaths = tbl_to_str(settings.libpath, "/libpath:\"", "\" ")
580
 
        if settings.debug > 0 then flags = flags .. "/DEBUG " end
581
 
        local exec = "link /nologo /incremental:no " .. flags .. libpaths .. libs .. " /OUT:" .. output .. " " .. input
582
 
        if verbose > 0 then print(exec) end
583
 
        return os.execute(exec)
584
 
end
585
 
 
586
 
if platform == "win32" then
587
 
        compile_cxx = compile_cxx_cl
588
 
        compile_c = compile_c_cl
589
 
        link = link_cl
590
 
        lib = lib_cl
591
 
        dll = dll_cl
592
 
else
593
 
        compile_cxx = compile_cxx_gcc
594
 
        compile_c = compile_c_gcc
595
 
        link = link_gcc
596
 
        lib = lib_ar
597
 
        dll = dll_gcc
598
 
end