root/trunk/sss/clean.d

Revision 797, 5.1 kB (checked in by Gregor, 1 year ago)

*: Added better multi-compiler support.

Line 
1 /**
2  * DSSS commands "clean" an "distclean"
3  *
4  * Authors:
5  *  Gregor Richards
6  *
7  * License:
8  *  Copyright (c) 2006, 2007  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.conf;
37
38 import hcf.path;
39
40 /** A utility function to attempt removal of a file but not fail on error */
41 void tryRemove(char[] fn)
42 {
43     try {
44         std.file.remove(fn);
45     } catch (Exception e) {
46         // ignored
47     }
48 }
49
50 /** Clean a tree: Remove all empty directories in the tree */
51 void cleanTree(char[] dirn)
52 {
53     try {
54         rmdir(dirn);
55         cleanTree(getDirName(dirn));
56     } catch (Exception e) {
57         // ignored
58     }
59 }
60
61 /** The entry function to the DSSS "clean" command */
62 int clean(DSSSConf conf = null) {
63     // fairly simple, get rid of easy things - dsss_objs and dsss_imports
64     if (exists("dsss_objs"))
65         rmRecursive("dsss_objs");
66     if (exists("dsss_imports"))
67         rmRecursive("dsss_imports");
68     if (exists("dsss_docs"))
69         rmRecursive("dsss_docs");
70     return 0;
71 }
72
73 /** The entry function to the DSSS "distclean" command */
74 int distclean(DSSSConf conf = null)
75 {
76     // get the configuration
77     if (conf is null)
78         conf = readConfig(null);
79    
80     // distclean implies clean
81     int res = clean(conf);
82     if (res) return res;
83     writefln("");
84    
85     // get the sources
86     char[][] buildSources = sourcesByElems(null, conf);
87    
88     // then go through and delete actual files
89     foreach (build; buildSources) {
90         char[][char[]] settings = conf.settings[build];
91        
92         // basic info
93         char[] type = settings["type"];
94         char[] target = settings["target"];
95        
96         // tell what we're doing
97         writefln("Removing %s", target);
98        
99         // do the preclean step
100         if ("preclean" in settings) {
101             dsssScriptedStep(conf, settings["preclean"]);
102         }
103        
104         if (type == "library" || type == "sourcelibrary") {
105             if (targetGNUOrPosix()) {
106                 // first remove the static library
107                 tryRemove("lib" ~ target ~ ".a");
108
109                 // then any testing binary
110                 tryRemove("test_" ~ target);
111                
112                 // then remove the shared libraries
113                 char[] shlibname = getShLibName(settings);
114                 char[][] shortshlibnames = getShortShLibNames(settings);
115                
116                 tryRemove(shlibname);
117                 foreach (ssln; shortshlibnames) {
118                     tryRemove(ssln);
119                 }
120                
121             } else if (targetVersion("Windows")) {
122                 // first remove the static library
123                 tryRemove(target ~ ".lib");
124
125                 // then any testing binary
126                 tryRemove("test_" ~ target);
127                
128                 // then remove the shared libraries
129                 char[] shlibname = getShLibName(settings);
130                 char[][] shortshlibnames = getShortShLibNames(settings);
131                
132                 tryRemove(shlibname);
133                 foreach (ssln; shortshlibnames) {
134                     tryRemove(ssln);
135                 }
136             } else {
137                 assert(0);
138             }
139            
140         } else if (type == "binary") {
141             if (targetVersion("Posix")) {
142                 tryRemove(target);
143             } else if (targetVersion("Windows")) {
144                 tryRemove(target ~ ".exe");
145             } else {
146                 assert(0);
147             }
148            
149         } else if (type == "subdir") {
150             // recurse
151             char[] origcwd = getcwd();
152             chdir(build);
153             int cleanret = distclean();
154             if (cleanret) return cleanret;
155             chdir(origcwd);
156            
157         }
158        
159         // do the postclean step
160         if ("postclean" in settings) {
161             dsssScriptedStep(conf, settings["postclean"]);
162         }
163        
164         writefln("");
165     }
166    
167     // and the lastbuild file if it exists
168     if (exists(configLBName)) {
169         tryRemove(configLBName);
170     }
171    
172     return 0;
173 }
Note: See TracBrowser for help on using the browser.