~ace17/midee/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/dmd -run
import std.stdio;
import std.process;
import std.array;

typedef string syspath; // a path in the form of the underlying OS.
typedef string genpath; // a path separated by "|"

class BuildException : public Throwable
{
  public:
  this(string s)
  {
    super(s);
    sMsg = s;
  }

  const string sMsg;
}

void Exec(string cmdLine)
{
  writefln("> %s", cmdLine);
  int ret = system(cmdLine);
  if(ret != 0)
    throw new BuildException("compilation failed");
}


/**
 * @brief custom "explode" function, using '|' as separator.
 */
string[] explode(genpath p)
{
  string[] r;
  string curr;
  foreach(c;p)
  {
    if(c == '|')
    {
      r ~= curr;
      curr = "";
    }
    else
      curr ~= c;
  }

  if(curr != "")
    r ~= curr;
  return r;
}

syspath toSyspath(genpath p)
{
  string[] slices = explode(p);
  syspath r;
  foreach(s;slices)
    r = cast(syspath)std.path.join(r, s);
  return r;
}

void CompileAsD(genpath[] files, genpath target, genpath[] linklibs, genpath[] includeDirs)
{
  string cmdLine;
  cmdLine ~= "dmd";

  cmdLine ~= " -of";
  cmdLine ~= "\"" ~ toSyspath(target) ~ "\"";

  foreach(file;files)
  {
    syspath p = toSyspath(file);
    cmdLine ~= " ";
    cmdLine ~= "\"" ~ p ~ "\"";
  }

  foreach(lib;linklibs)
  {
    cmdLine ~= " ";
    cmdLine ~= "-L-l\"" ~ lib ~ "\"";
  }

  foreach(dir;includeDirs)
  {
    cmdLine ~= " ";
    cmdLine ~= "-I\"" ~ toSyspath(dir) ~ "\"";
  }

  Exec(cmdLine);
}

void Build(Project project)
{
  writeln("Building : ", toSyspath(project._target));

  CompileAsD(project._srcs, project._target, project._linklibs, project._includedirs);
}

genpath G(string s)
{
  return cast(genpath)s;
}

struct Project
{
  genpath _srcs[];
  genpath _linklibs[]; // libraries to link with.
  genpath _target;
  genpath _includedirs[];

  void addSource(genpath sourceFile)
  {
    _srcs ~= sourceFile;
  }

  void addLinkLib(genpath linkLib)
  {
    _linklibs ~= linkLib;
  }

  void addIncludeDir(genpath includedir)
  {
    _includedirs ~= includedir;
  }

  void setTarget(genpath target)
  {
    _target = target;
  }
}

int main(string[] args)
{
  try
  {
    mangled_main(args);
    return 0;
  }
  catch(BuildException e)
  {
    writeln("Build failed : ", e.sMsg);
    return 1;
  }
}

int mangled_main(string[] args)
{
  Project project;

  bool bHasTarget = false;

  foreach(arg;args)
  {
    if(arg == "--windows")
    {
      project.addSource(G("src|midee_winmm.d"));
      project.addSource(G("src|native|win32|mmsystem.d"));
      project.addSource(G("winmm.lib"));
      project.addIncludeDir(G("src|native"));
      bHasTarget = true;
    }
    else if(arg == "--linux")
    {
      project.addSource(G("src|midee_alsa.d"));
      project.addSource(G("src|native|alsa.d"));
      project.addLinkLib(G("asound"));
      bHasTarget = true;
    }
  }

  if(!bHasTarget)
  {
    writeln("No target specified (--windows).");
    return 0;
  }
  project.addSource(G("src|midee.d"));
  project.addSource(G("src|queue.d"));
  project.addSource(G("test|test.d"));
  project.setTarget(G("bin|test.exe"));

  Build(project);

  return 0;
}

// vim: set ts=2 sw=2 expandtab