Skip to content

Instantly share code, notes, and snippets.

@kent013 kent013/gist:2035173
Created Mar 14, 2012

Embed
What would you like to do?
Objective-c ARC/Non-ARC compiler flag adder for tottepost
<?php
if(isset($_SERVER['argv'][1]) == false){
die("Please set xcode project file url¥n");
}
$filename = $_SERVER['argv'][1];
if(file_exists($filename) == false){
die("Project file not exist¥n");
}
$dir = realpath(dirname(dirname($filename)));
$file = file($filename);
$files = array();
//list sources
foreach($file as $line){
if(preg_match('/([a-zA-Z0-9-_+]+?¥.m) in Sources/', $line, $regs)){
$fn = $regs[1];
$files[$fn] = array("filename" => $fn, "arc" => true);
}
}
//mark non-arc files
foreach($file as $line){
if(preg_match('/([a-zA-Z0-9-_+]+?¥.m) in Sources.+(-fno-objc-arc)/', $line, $regs)){
$fn = $regs[1];
$files[$fn]["arc"] = false;
}
}
$i = 0;
//get real path
foreach($files as $k => $file){
$fn = $file["filename"];
$realpaths = find($dir, $fn);
$files[$k]["realpath"] = $realpaths[0];
$i ++ ;
}
//check flag existance
foreach($files as $k => $file){
if(isset($file["realpath"]) == false){
continue;
}
$result = grep($file["realpath"], "__has_feature(objc_arc)");
if($result){
$files[$k]["flag"] = true;
continue;
}
$files[$k]["flag"] = false;
}
$nonArcFlag = <<<EOD
#if __has_feature(objc_arc)
#error This file must be compiled with Non-ARC. use -fno-objc_arc flag (or convert project to Non-ARC)
#endif
EOD;
$arcFlag = <<<EOD
#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
EOD;
//insert flag
foreach($files as $k => $file){
if(isset($file["realpath"]) == false){
continue;
}
//ignore files
if(!strpos($file["realpath"], 'PhotoSubmitter') ||
strpos($file["realpath"], 'KissXML') ||
strpos($file["realpath"], 'Libraries/Evernote')){
continue;
}
$content = file_get_contents($file["realpath"]);
if($file["flag"]){
$content = preg_replace("/¥n#if.*?!?.*?__has_feature¥(objc_arc¥).+?#endif¥n?/ms", '', $content);
}
$pos = strpos($content, "#import");
if($pos === FALSE){
continue;
}
$flag = $nonArcFlag;
if($file["arc"]){
$flag = $arcFlag;
}
$content = mb_substr($content, 0, $pos) . $flag . "¥n¥n" . mb_substr($content, $pos);
file_put_contents($file["realpath"], $content);
}
//-----------------------------------------------------------------------------
// util functions
//-----------------------------------------------------------------------------
/**
* grep file
*/
function grep($file, $pattern){
$file = escapeshellcmd($file);
$results = shell_exec("head -n 40 $file | grep '$pattern'");
return $results;
}
/**
* find files matching a pattern
*/
function find($dir, $pattern){
$dir = escapeshellcmd($dir);
$files = shell_exec("find $dir -name '$pattern' -print");
$files = explode("¥n", trim($files));
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.