root/branches/bud/sss/clean.d

Revision 252, 4.9 kB (checked in by Gregor, 2 years ago)

sss/clean.d: Fixed a possible bug on Windows with file removal.

Line 
1 /**
2  * DSSS commands "clean" an "distclean"
3  *
4  * Authors:
5  *  Gregor Richards
6  *
7  * License:
8  *  Copyright (c) 2006  Gregor Richards
9  * 
10  *  Permission is hereby granted, free of charge, to any person obtaining a
11  *  copy of this software and associated documentation files (the "Software"),
12  *  to deal in the Software without restriction, including without limitation
13  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  *  and/or sell copies of the Software, and to permit persons to whom the
15  *  Software is furnished to do so, subject to the following conditions:
16  * 
17  *  The above copyright notice and this permission notice shall be included in
18  *  all copies or substantial portions of the Software.
19  * 
20  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  *  DEALINGS IN THE SOFTWARE.
27  */
28
29 module sss.clean;
30
31 import std.file;
32 import std.path;
33 import std.stdio;
34 import std.string;
35
36 import sss.build;
37 import sss.conf;
38
39 /** A utility function to attempt removal of a file but not fail on error */
40 void tryRemove(char[] fn)
41 {
42     try {
43         std.file.remove(fn);
44     } catch (Exception e) {
45         // ignored
46     }
47 }
48
49 /** The entry function to the DSSS "clean" command */
50 int clean(DSSSConf conf = null) {
51     // fairly simple
52     foreach (dire; listdir(".")) {
53         char[] ext = std.string.tolower(getExt(dire));
54         if (ext == "o" ||
55             ext == "obj") {
56             writefln("Removing %s", dire);
57             tryRemove(dire);
58         }
59     }
60    
61     return 0;
62 }
63
64 /** The entry function to the DSSS "distclean" command */
65 int distclean(DSSSConf conf = null)
66 {
67     // get the configuration
68     if (conf is null)
69         conf = readConfig(null);
70    
71     // distclean implies clean
72     int res = clean(conf);
73     if (res) return res;
74     writefln("");
75    
76     // get the sources
77     char[][] buildSources = sourcesByElems(null, conf);
78    
79     // then go through and delete actual files
80     foreach (build; buildSources) {
81         char[][char[]] settings = conf.settings[build];
82        
83         // basic info
84         char[] type = settings["type"];
85         char[] target = settings["target"];
86        
87         // tell what we're doing
88         writefln("Removing %s", target);
89        
90         // do the preclean step
91         if ("preclean" in settings) {
92             dsssScriptedStep(conf, settings["preclean"]);
93         }
94        
95         if (type == "library") {
96             // remove the .di files ...
97             char[][] files = targetToFiles(build, conf);
98             foreach (file; files) {
99                 tryRemove(file ~ "i");
100                 tryRemove(file ~ "i0");
101             }
102            
103             version (GNU_or_Posix) {
104                 // first remove the static library
105                 tryRemove("libS" ~ target ~ ".a");
106                
107                 // then remove the shared libraries
108                 char[] shlibname = getShLibName(settings);
109                 char[][] shortshlibnames = getShortShLibNames(settings);
110                
111                 tryRemove(shlibname);
112                 foreach (ssln; shortshlibnames) {
113                     tryRemove(ssln);
114                 }
115                
116             } else version (Windows) {
117                 // first remove
118                 tryRemove("S" ~ target ~ ".lib");
119                
120                 // then remove the shared libraries
121                 char[] shlibname = getShLibName(settings);
122                 char[][] shortshlibnames = getShortShLibNames(settings);
123                
124                 tryRemove(shlibname);
125                 foreach (ssln; shortshlibnames) {
126                     tryRemove(ssln);
127                 }
128             } else {
129                 static assert(0);
130             }
131            
132         } else if (type == "binary") {
133             version (Posix) {
134                 tryRemove(target);
135             } else version (Windows) {
136                 tryRemove(target ~ ".exe");
137             } else {
138                 static assert(0);
139             }
140            
141         } else if (type == "subdir") {
142             // recurse
143             char[] origcwd = getcwd();
144             chdir(build);
145             int cleanret = distclean();
146             if (cleanret) return cleanret;
147             chdir(origcwd);
148            
149         }
150        
151         // do the postclean step
152         if ("postclean" in settings) {
153             dsssScriptedStep(conf, settings["postclean"]);
154         }
155        
156         writefln("");
157     }
158    
159     // and the lastbuild file if it exists
160     if (exists(configLBName)) {
161         tryRemove(configLBName);
162     }
163    
164     return 0;
165 }
Note: See TracBrowser for help on using the browser.