1# Arguments that this derivation gets when it is created with `callPackage`
2{
3 stdenv,
4 lib,
5 symlinkJoin,
6 makeWrapper,
7 runCommand,
8 file,
9}:
10
11open-watcom:
12
13let
14 wrapper =
15 { }:
16 let
17 archToBindir =
18 with stdenv.hostPlatform;
19 if isx86 then
20 "bin"
21 else if isAarch then
22 "arm"
23 # we don't support running on AXP
24 # don't know what MIPS, PPC bindirs are called
25 else
26 throw "Don't know where ${system} binaries are located!";
27
28 binDirs =
29 with stdenv.hostPlatform;
30 if isWindows then
31 [
32 (lib.optionalString is64bit "${archToBindir}nt64")
33 "${archToBindir}nt"
34 (lib.optionalString is32bit "${archToBindir}w")
35 ]
36 else if (isDarwin) then
37 [
38 (lib.optionalString is64bit "${archToBindir}o64")
39 # modern Darwin cannot execute 32-bit code anymore
40 (lib.optionalString is32bit "${archToBindir}o")
41 ]
42 else
43 [
44 (lib.optionalString is64bit "${archToBindir}l64")
45 "${archToBindir}l"
46 ];
47 # TODO
48 # This works good enough as-is, but should really only be targetPlatform-specific
49 # but we don't support targeting DOS, OS/2, 16-bit Windows etc Nixpkgs-wide so this needs extra logic
50 includeDirs =
51 with stdenv.hostPlatform;
52 [
53 "h"
54 ]
55 ++ lib.optional isWindows "h/nt"
56 ++ lib.optional isLinux "lh";
57 listToDirs = list: lib.strings.concatMapStringsSep ":" (dir: "${placeholder "out"}/${dir}") list;
58 name = "${open-watcom.passthru.prettyName}-${open-watcom.version}";
59 in
60 symlinkJoin {
61 inherit name;
62
63 paths = [ open-watcom ];
64
65 nativeBuildInputs = [ makeWrapper ];
66
67 postBuild = ''
68 mkdir $out/bin
69
70 for binDir in ${lib.strings.concatStringsSep " " binDirs}; do
71 for exe in $(find ${open-watcom}/$binDir \
72 -type f -executable \
73 ${lib.optionalString stdenv.hostPlatform.isLinux "-not -iname '*.so' -not -iname '*.exe'"} \
74 ); do
75 if [ ! -f $out/bin/$(basename $exe) ]; then
76 makeWrapper $exe $out/bin/$(basename $exe) \
77 --set WATCOM ${open-watcom} \
78 --prefix PATH : ${listToDirs binDirs} \
79 --set EDPATH ${open-watcom}/eddat \
80 --set INCLUDE ${listToDirs includeDirs}
81 fi
82 done
83 done
84 '';
85
86 passthru = {
87 unwrapped = open-watcom;
88 tests =
89 let
90 wrapped = wrapper { };
91 in
92 {
93 simple = runCommand "${name}-test-simple" { nativeBuildInputs = [ wrapped ]; } ''
94 cat <<EOF >test.c
95 #include <stdio.h>
96 int main() {
97 printf ("Testing OpenWatcom C89 compiler.\n");
98 return 0;
99 }
100 EOF
101 cat test.c
102 wcl386 -fe=test_c test.c
103 # Only test execution if hostPlatform is targetable
104 ${lib.optionalString (!stdenv.hostPlatform.isDarwin && !stdenv.hostPlatform.isAarch) "./test_c"}
105
106 cat <<EOF >test.cpp
107 #include <string>
108 #include <iostream>
109 int main() {
110 std::cout << "Testing OpenWatcom C++ library implementation." << std::endl;
111 watcom::istring HELLO ("HELLO");
112 if (HELLO != "hello") {
113 return 1;
114 }
115 if (HELLO.find ("ello") != 1) {
116 return 2;
117 }
118 return 0;
119 }
120 EOF
121 cat test.cpp
122 wcl386 -fe=test_cpp test.cpp
123 # Only test execution if hostPlatform is targetable
124 ${lib.optionalString (!stdenv.hostPlatform.isDarwin && !stdenv.hostPlatform.isAarch) "./test_cpp"}
125 touch $out
126 '';
127 cross =
128 runCommand "${name}-test-cross"
129 {
130 nativeBuildInputs = [
131 wrapped
132 file
133 ];
134 }
135 ''
136 cat <<EOF >test.c
137 #include <stdio.h>
138 int main() {
139 printf ("Testing OpenWatcom cross-compilation.\n");
140 return 0;
141 }
142 EOF
143 cat test.c
144
145 echo "Test compiling"
146 wcl386 -bcl=linux -fe=linux test.c
147 wcl386 -bcl=nt -fe=nt test.c
148 wcl386 -bcl=dos4g -fe=dos4g test.c
149 wcl -bcl=windows -fe=windows test.c
150 wcl -bcl=dos -fe=dos test.c
151
152 echo "Test file format"
153 file ./linux
154 file ./linux | grep "32-bit" | grep -q "Linux"
155 file ./nt.exe
156 file ./nt.exe | grep "PE32" | grep -q "Windows"
157 file ./dos4g.exe
158 file ./dos4g.exe | grep "MS-DOS" | grep -q "executable, LE"
159 file ./windows.exe
160 file ./windows.exe | grep "MS-DOS" | grep -q "Windows 3.00"
161 file ./dos.exe
162 file ./dos.exe | grep "MS-DOS" | grep -v "LE" | grep -qv "Windows 3."
163 touch $out
164 '';
165 };
166 };
167
168 inherit (open-watcom) meta;
169 };
170in
171lib.makeOverridable wrapper