-
Notifications
You must be signed in to change notification settings - Fork 10
/
demo.sh
executable file
·175 lines (141 loc) · 3.62 KB
/
demo.sh
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
#!/bin/bash
cat > blocktest.m << EOF
#include <stdio.h>
int main() {
void (^hello)(void) = ^(void) {
printf("Hello, block!\n");
};
hello();
return 0;
}
EOF
cat > helloGCD_objc.m << EOF
#include <dispatch/dispatch.h>
#import <stdio.h>
#import "Fraction.h"
int main( int argc, const char *argv[] ) {
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
Fraction *frac = [[Fraction alloc] init];
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
dispatch_sync(queue, ^{
printf( "The fraction is: " );
[frac print];
printf( "\n" );
});
dispatch_release(queue);
[frac release];
return 0;
}
EOF
cat > helloGCD_ARC_objc.m << EOF
#include <dispatch/dispatch.h>
#import <stdio.h>
#import "Fraction.h"
int main( int argc, const char *argv[] ) {
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
Fraction *frac = [[Fraction alloc] init];
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
dispatch_sync(queue, ^{
printf( "The fraction is: " );
[frac print];
printf( "\n" );
});
if (__has_feature(objc_arc)) {
printf("ARC is working.");
return 0;
} else {
printf("ARC is not working.");
return -1;
}
}
EOF
cat > Fraction.h << EOF
#import <Foundation/NSObject.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
EOF
cat > Fraction.m << EOF
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end
EOF
# ======================================================================
# COMPILE USING THE FOLLOWING COMMAND LINES, OR CREATE A MAKEFILE
# ======================================================================
# Set compiler
if [ -x "$(command -v ${CC})" ]; then
echo "Using CC from environment variable: ${CC}"
else
if [ -x "$(command -v clang-9)" ]; then
CC=clang-9
elif [ -x "$(command -v clang-11)" ]; then
CC=clang-11
elif [ -x "$(command -v clang-8)" ]; then
CC=clang-8
elif [ -x "$(command -v clang-6.0)" ]; then
CC=clang-6.0
elif [ -x "$(command -v clang)" ]; then
CC=clang
else
echo 'Error: clang seems not to be in PATH. Please check your $PATH.' >&2
exit 1
fi
fi
# Using COMMAND LINE
echo "Compiling and running blocks demo."
${CC} `gnustep-config --objc-flags` `gnustep-config --objc-libs` -lobjc blocktest.m
./a.out
echo "Compiling and running Grand Central Dispatch demo."
${CC} `gnustep-config --objc-flags` `gnustep-config --objc-libs` -lobjc -ldispatch -lgnustep-base Fraction.m helloGCD_objc.m
./a.out
# Using MAKEFILE
cat > GNUmakefile << EOF
include \$(GNUSTEP_MAKEFILES)/common.make
APP_NAME = FractionDemo
LDFLAGS = -ldispatch
FractionDemo_OBJC_FILES = Fraction.m helloGCD_objc.m
include \$(GNUSTEP_MAKEFILES)/application.make
EOF
echo "Compiling and running Fraction demo (makefile compilation)."
make
openapp ./FractionDemo.app
# Using MAKEFILE with enabled ARC
cat > GNUmakefile << EOF
include \$(GNUSTEP_MAKEFILES)/common.make
OBJCFLAGS = -fobjc-arc
LDFLAGS = -ldispatch
APP_NAME = FractionArcDemo
FractionArcDemo_OBJC_FILES = Fraction.m helloGCD_ARC_objc.m
include \$(GNUSTEP_MAKEFILES)/application.make
EOF
echo "Compiling and running Fraction ARC demo (makefile compilation)."
make
openapp ./FractionArcDemo.app