| 1 |
/* |
| 2 |
Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Written by: Gregory John Casamento <[email protected]> |
| 5 |
Date: 2022 |
| 6 |
|
| 7 |
This file is part of the GNUstep XCode Library |
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This library is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with this library; if not, write to the Free |
| 21 |
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 |
Boston, MA 02110 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#import <Foundation/NSUUID.h> |
| 26 |
|
| 27 |
#import "GSXCBuildContext.h" |
| 28 |
#import "NSString+PBXAdditions.h" |
| 29 |
#import "xcsystem.h" |
| 30 |
|
| 31 |
#import <stdlib.h> |
| 32 |
#ifndef _MSC_VER |
| 33 |
#import <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
NSString *contextString() |
| 37 |
{ |
| 38 |
NSString *result = @"\n# Context variables\n\n"; |
| 39 |
GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext]; |
| 40 |
NSDictionary *dict = [ctx currentContext]; |
| 41 |
NSArray *keys = [dict allKeys]; |
| 42 |
NSEnumerator *en = [keys objectEnumerator]; |
| 43 |
NSString *k = nil; |
| 44 |
|
| 45 |
while ((k = [en nextObject]) != nil) |
| 46 |
{ |
| 47 |
if (![k isEqualToString: @"OUTPUT_FILES"]) // exclude some vars |
| 48 |
{ |
| 49 |
id v = [dict objectForKey: k]; |
| 50 |
if ([v isKindOfClass: [NSString class]]) |
| 51 |
{ |
| 52 |
if ([v containsString: @"%"] == NO) |
| 53 |
{ |
| 54 |
if ([v containsString: @"\""] ) // val is already quoted |
| 55 |
{ |
| 56 |
result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=%@\n", k, v]]; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=\"%@\"\n", k, v]]; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
result = [result stringByAppendingString: @"# Done with context setup...\n\n"]; |
| 68 |
|
| 69 |
return result; |
| 70 |
} |
| 71 |
|
| 72 |
NSInteger xcsystem(NSString *cmd) |
| 73 |
{ |
| 74 |
GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext]; |
| 75 |
NSDictionary *dict = [ctx config]; |
| 76 |
NSString *setupScript = [dict objectForKey: @"setupScript"]; |
| 77 |
NSInteger r = 0; |
| 78 |
|
| 79 |
if(setupScript == nil) |
| 80 |
{ |
| 81 |
#ifdef _WIN32 |
| 82 |
NSString *win_cmd = [cmd execPathForString]; |
| 83 |
|
| 84 |
NSDebugLog(@"win_cmd = %@", win_cmd); |
| 85 |
r = system([win_cmd cString]); |
| 86 |
#else |
| 87 |
r = system([cmd cString]); |
| 88 |
#endif |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
NSString *scriptFormat = @"#!/bin/bash\n" |
| 93 |
@"set -eo pipefail\n" |
| 94 |
@"shopt -s inherit_errexit\n\n" |
| 95 |
@"%@\n\n" |
| 96 |
@"exit $?\n"; |
| 97 |
NSString *body = @""; |
| 98 |
NSString *script = nil; |
| 99 |
NSString *c = [cmd stringByReplacingOccurrencesOfString: @"$(SRCROOT)" withString: @"."]; |
| 100 |
NSUUID *uuid = [NSUUID UUID]; |
| 101 |
|
| 102 |
body = [body stringByAppendingString: [NSString stringWithFormat: @". %@ > /dev/null\n", setupScript]]; |
| 103 |
body = [body stringByAppendingString: contextString()]; |
| 104 |
body = [body stringByAppendingString: [NSString stringWithFormat: @"%@\n", c]]; |
| 105 |
|
| 106 |
script = [NSString stringWithFormat: scriptFormat, body]; |
| 107 |
|
| 108 |
NSString *filename = [NSString stringWithFormat: @"bt_%@.sh", [uuid UUIDString]]; // [script hash]]; |
| 109 |
NSString *scriptCmd = [NSString stringWithFormat: @"./build/%@", filename]; |
| 110 |
BOOL f = [script writeToFile: scriptCmd |
| 111 |
atomically: YES |
| 112 |
encoding: NSUTF8StringEncoding |
| 113 |
error: NULL]; |
| 114 |
if (f) |
| 115 |
{ |
| 116 |
#ifdef _WIN32 |
| 117 |
NSString *win_cmd = [scriptCmd execPathForString]; |
| 118 |
NSDebugLog(@"win_cmd = %@", win_cmd); |
| 119 |
r = system([win_cmd cString]); |
| 120 |
#else |
| 121 |
r = system([scriptCmd cString]); |
| 122 |
#endif |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
NSLog(@"Failed to write out temporary script %@", scriptCmd); |
| 127 |
} |
| 128 |
} |
| 129 |
return r; |
| 130 |
} |
| 131 |
|
| 132 |
void xcputs(const char *str) |
| 133 |
{ |
| 134 |
puts(str); |
| 135 |
fflush(stdout); |
| 136 |
} |
| 137 |
|
| 138 |
void xcput_string(NSString *str) |
| 139 |
{ |
| 140 |
xcputs([str cString]); |
| 141 |
} |
| 142 |
|
| 143 |
int xcprintf(const char *format, ...) |
| 144 |
{ |
| 145 |
int result; |
| 146 |
va_list args; |
| 147 |
|
| 148 |
va_start(args, format); |
| 149 |
result = vprintf(format, args); |
| 150 |
va_end(args); |
| 151 |
|
| 152 |
fflush(stdout); |
| 153 |
|
| 154 |
return result; |
| 155 |
} |