4fad21a8368db8a26c045b355618657c54c9e64f
[lwext4.git] / cscript
1 # -*- mode: python -*-
2 #
3 #    Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
4 #
5 #    This program is free software; you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation; either version 2 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program; if not, write to the Free Software
17 #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19
20 def make_toolchain(filename, arch, sdk_prefix, sdk):
21     with open(filename, 'w') as f:
22         flags = "-arch %s" % arch
23         print("set(CMAKE_SYSTEM_NAME Darwin)", file=f)
24         print("set(CMAKE_SYSTEM_PROCESSOR %s)" % arch, file=f)
25         print("set(CMAKE_C_COMPILER    gcc)", file=f)
26         print("set(CMAKE_CXX_COMPILER  g++)", file=f)
27         print("set(AS                  as)", file=f)
28         print("set(AR                  as)", file=f)
29         print("set(OBJCOPY             objcopy)", file=f)
30         print("set(OBJDUMP             objdump)", file=f)
31         print("set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})", file=f)
32         print("set(CMAKE_OSX_SYSROOT %s/MacOSX%s.sdk)" % (sdk_prefix, sdk), file=f)
33         print('set(CMAKE_C_FLAGS   "-std=gnu99 -fdata-sections -ffunction-sections %s" CACHE INTERNAL "c compiler flags")' % flags, file=f)
34         print('set(CMAKE_CXX_FLAGS "-fdata-sections -ffunction-sections %s" CACHE INTERNAL "cxx compiler flags")' % flags, file=f)
35         print('set(CMAKE_ASM_FLAGS "" CACHE INTERNAL "asm compiler flags")', file=f)
36         print('set(CMAKE_EXE_LINKER_FLAGS "-dead_strip" CACHE INTERNAL "exe link flags")', file=f)
37         print('SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -ggdb3" CACHE INTERNAL "c debug compiler flags")', file=f)
38         print('SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb3" CACHE INTERNAL "cxx debug compiler flags")', file=f)
39         print('SET(CMAKE_ASM_FLAGS_DEBUG "-g -ggdb3" CACHE INTERNAL "asm debug compiler flags")', file=f)
40         print('SET(CMAKE_C_FLAGS_RELEASE "-O2 -g -ggdb3" CACHE INTERNAL "c release compiler flags")', file=f)
41         print('SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -g -ggdb3" CACHE INTERNAL "cxx release compiler flags")', file=f)
42         print('SET(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm release compiler flags")', file=f)
43
44
45 def build(target, options):
46     if target.platform == 'linux':
47         type = 'generic'
48         build_suffix = type
49         blockdev = 'linux'
50         ext = 'a'
51         device = 'dev'
52     elif target.platform == 'osx':
53         if target.arch == 'x86_64':
54             type = 'osx'
55         elif target.arch == 'arm64':
56             type = 'osx-arm64'
57         build_suffix = 'osx'
58         make_toolchain('toolchain/%s.cmake' % type, target.arch, target.sdk_prefix, target.sdk)
59         blockdev = 'linux'
60         ext = 'dylib'
61         device = 'dev'
62     elif target.platform == 'windows':
63         type = 'mingw' if target.bits == 64 else 'mingw-32'
64         build_suffix = type
65         blockdev = 'windows'
66         ext = 'dll'
67         device = 'windows'
68
69     target.command('mkdir -p %s/include/lwext4' % target.directory)
70     target.command('cp -r include/* %s/include/lwext4' % target.directory)
71     target.command('make %s' % type)
72     target.command('make -j%d -C build_%s' % (target.parallel, build_suffix))
73     target.command('cp -r build_%s/include/generated %s/include/lwext4' % (build_suffix, target.directory))
74     target.command('cp blockdev/%s/file_%s.h %s/include/lwext4' % (blockdev, device, target.directory))
75     target.command('mkdir -p %s/lib' % target.directory)
76     target.command('cp build_%s/src/liblwext4.%s %s/lib' % (build_suffix, ext, target.directory))
77     target.command('cp build_%s/blockdev/libblockdev.%s %s/lib' % (build_suffix, ext, target.directory))