Since there is no Xcode script variable for "current project directory," how can you create a script menu item to run the Clang Static Analyzer on your current project from Xcode?
From the XCode script menu item, "Edit User Scripts" enter the following script:
#!/bin/bash
result=$( osascript << END
tell application "Xcode"
tell active project document
set projectPath to path as string
end tell
end tell
return projectPath
END
)
cd "$result"
/Developer/clangchecker/scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0
Obviously, you will need to adjust the path to your install of Clang, and adjust to the version of the SDK you are using.
Remember to do a "Clean All" immediately before using scan-build, or the results may be incomplete.
FYI, Xcode 3.2 (Snow Leopard only I believe) includes the Clang Static Analyzer in the "Build and Analyze" menu option.
http://iosdevelopertips.com/xcode/static-code-analysis-clang-and-xcode-3-2.html
One downside of Xcode 3.2 (aside from it only working on Snow Leopard) is that the v2.x Simulators don't seem to work - in fact, I've seen posts indicating that v2.x builds are not supported at all.
I believe the ${PROJECT_DIR} environment variable is what you want for the directory of the project running a build-phase script.
Either use the version bundled in XCode 3.2+, or download a newer version at https://clang-analyzer.llvm.org/ then see some additional instructions at https://clang-analyzer.llvm.org/xcode.html to switch XCode to that downloaded version.
Related
I am facing a problem running older versions of Xcode on newer MacOS versions.
For example, Xcode 13 on MacOS Ventura.
The solution is very simple. If you have the older version downloaded in your Applications folder for example, lets say 12.5.1 version, you just need to:
Open Terminal
Open Applications folder in Finder
Drag the Xcode app into Terminal so it gets its path
Then add this next to it: /Contents/MacOS/Xcode, so the full command will be something like /Applications/Xcode-12.5.1.app/Contents/MacOS/Xcode
Press enter to run the command
Now you should be able to run it. You will note that when you open this version of Xcode, the Terminal will open too, but don't close Terminal because it will close the Xcode too.
Here you can find older Xcode versions.
Change the paths to OLD/NEW Xcodes and run script. The script will change the build version of the old Xcode to the new one, run it and restore. Script needs to be run once, after that Xcode can be opened via double click
Works on macOS Monterey for Xcode 12.5.1 and Ventura for Xcode 13
#!/bin/sh
set -euo pipefail
# Set the paths to your Old/New Xcodes
OLD_XCODE="/Applications/Xcode_13.4.1.app" # or /Applications/Xcode_12.5.1.app on Monterey
NEW_XCODE="/Applications/Xcode.app" # To get build number
# Get New Xcode build number
OLD_XCODE_BUILD=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${OLD_XCODE}/Contents/Info.plist)
NEW_XCODE_BUILD=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${NEW_XCODE}/Contents/Info.plist)
echo The Old Xcode build version is $OLD_XCODE_BUILD
echo The New Xcode build version is $NEW_XCODE_BUILD
# Change Old Xcode build version to New Xcode
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${NEW_XCODE_BUILD}" ${OLD_XCODE}/Contents/Info.plist
# Open Old Xcode (system will check build version and cache it)
open $OLD_XCODE
# Revert Old's Xcode's build version
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${OLD_XCODE_BUILD}" ${OLD_XCODE}/Contents/Info.plist
This is how you get your xcode's current build version.
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Applications/Xcode_12.4.app/Contents/Info.plist
If you are looking for a solution without using terminal every time, here it is:
Follow https://stackoverflow.com/a/69995053/14199447
Create a bash file with this content
#!/bin/bash
/Applications/Xcode-12.5.1.app/Contents/MacOS/Xcode
Open terminal, run chmod 700 YourBashFile.sh
Change the default opening app of YourBashFile to terminal.
Follow step 1 and 2 of this https://apple.stackexchange.com/a/407885 to create an executable application which you can put on your Dock. After this you should be able to use the new app like any other app.
I have an existing App, basically a shopping list app, to which I'm trying to add some sweet sweet SwiftUI lovin.
My issue is the real time preview updating doesn't work - the warning "Automatic preview updating paused" continually shows. I hit the resume button, it builds the app, it shows the current view, and that warning immediately shows again. I can never see changes to the code reflected in the canvas without using the resume button.
This is happening in Xcode 11.1, and 11.2 beta 2. I can find literally no other mention of this either here on SO, and there's one thread with no answers on Apple's Dev forums.
If you're having custom Run Script Phases in Build Phases and you don't want (or can't) remove them, then try to check checkbox "Run script only when installing".
The problem with all the given answers is that you need to check or uncheck your script in debug mode if you want to make the preview work.
Here is a convenient alternative using the environment variables.
This is really simple
Embed all the content of your script in an if statement that check if we're using the preview or not. If we're in preview, then don't run the content of your script, otherwise, let's run it. And you don't have to sacrifice your script for release versions only.
Here is the template :
if [ $ENABLE_PREVIEWS == "NO" ]
then
# your code to execute here
else
echo "Skipping the script because of preview mode"
fi
And below a full example that I use to bump my build version number
# xcode-build-bump.sh
# #desc Auto-increment the build number every time the project is run.
# #usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"
# 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
if [ $ENABLE_PREVIEWS == "NO" ]
then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
else
echo "Skipping Bump of version"
echo $ENABLE_PREVIEWS
fi
I ended up sending in feedback to Apple, and they responded with a fix. I have a build script in the target that auto-increments the build number. If I remove that script then previewing works as intended.
So if you're having this issue remove anything in Target -> Build Phases -> Run Script and try again. The canvas preview should update as you would expect.
For me, Canvas did not work when I had Legacy Build System.
You can change it via,
File -> Project Settings (or Workspace Settings) -> Build System -> Choose "New Build System(Default).
As it says, it is the default option. If for any reason Legacy build system was chosen, Canvas won't work.
Edit on June 30, 2020:
We no longer have Legacy Build System in Xcode 12 beta.
If you use Xcode 13 or 14 with custom scripts, you must ensure that the script for install builds only is checked in.
What worked for me was to "clean" Xcode
On the Mac
Open Xcode
command+k (Clean console)
command+option+k (Reload console)
command+option+shift+k (Clean build folder)
Exit Xcode
From a terminal window, clean the derived data. I run the following based on where my Xcode is installed. I believe its the base location
rm -rf ~/Library/Developer/Xcode/DerivedData
Re-open xcode and it worked great!
In my experiements I found that ENABLE_PREVIEWS is always set to YES in a SwiftUI project. Instead I found that in normal builds Xcode sets TARGET_DEVICE_MODEL and in SwiftUI it does not.
So the solution is like the one described in this answer: https://stackoverflow.com/a/62216533/833197 but using a different variable.
On another note, setting anything in the Info.plist in a build script seems to be "too late" in recent Xcode versions. It will not be used until next build. Also you end up with a modified version control working copy of your files which might not be what you want.
To resolve this I have
Used a pre-build script in the build scheme instead
Generated a xcconfig with the build number and made it ignored by the version control system (git in my case).
The variables set in a xcconfig file can be referenced in the Info.plist file.
It's strange. But for me automatic preview always fails when I name projects using digits only (i. e. "111"). When naming using letters (with or without digits), everything is ok. 12.3 beta (12C5020f), Big Sur beta 11.1 (20C5048k).
For me it was because i had a pre-actions script in the Build section in the Edit Scheme screen, removing this script got the preview to work as intended
I updated to Xcode 5.0 and I can't seem to be able to include the GL/glew.h file. In previous Xcode version one had to set the base SDK to current OSX, but that option is no longer available (at least I can't find).
The file glew.h is in /usr/include/GL and I already set the header search paths to /usr/include, but it still doesn't work.
Can anyone help?
Thank you
Got it to work.
Find in the Project Build Settings > Search Paths > Always Search User Paths and set it to yes.
Also, I couldn't link with libGLEW so I had to go into the XCode.app and create a symbolic link to the the file.
Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib
It is definitely not a great solution but is a work-around for me.
Edit:
Here, I suppose that one is working with the Mac OSX 10.8 SKD. For other SDKS, the path should be another one.
In the terminal I created a link to my libGLEW.dylib (which was in /usr/lib) in this way
cd Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib
sudo ln -s /usr/lib/libGLEW.dylib libGLEW.dylib
Afterwards, back in XCode, in my project's "Build Settings" I set "Other Linker Flags" to -lGLEW
I have a xcode project that was created for osx snow leopard. All it was required to be built was to run
xcodebuild
from the command prompt. I am now trying to build the same project on OS X 10.7 and the following error happens when I run the same command
/Users/repo/trunk/main.c:31:10: fatal error: 'stdlib.h' file not found
What exactly is going on? I tried to follow the instructions from this question but stdlib.h is still missing.
How can I fix this problem?
Likely you are getting bit by the repackaging of everything inside /Applications/Xcode.app starting with Xcode 4.3. In Unix-like installations stdlib.h usually lives in /usr/include and your project likely expects it to be there as it was pre-Xcode 4.3. You can fix this by installing the command line tools, which includes the tools and the associated headers and libraries in their standard location.
The easiest way to install the command line tools is from inside Xcode. In the Xcode Preferences select the "Downloads" pane and then the "Components" tab. Push the "Install" button next to "Command Line Tools".
You need to run xcode-select to tell xcodebuild and xcrun which Xcode you want to use.
See this manpage.
I am interested to know how can I install native assemblar (I think also known as AS) on Mac OSX Lion. Someone suggested that I can use the Xcode to do it but I didn't see any options.Terminal windows is not able to recognize AS as command right now. Can someone provide more insight on this ? I am new to Mac/Unix environment so please provide help accordingly.
If you have Xcode installed, then you have as.There is no option to install specific packages from the Xcode installer.
as should be in /usr/bin/:
ls -l /usr/bin/as
If not, you probably have a problem with your Xcode installation.
Otherwise, also check your PATH environment variable, to ensure /usr/bin/ is in the list.
echo $PATH
If you've installed Xcode 4.3, it doesn't install the command line tools anymore. To get them, you have to launch Xcode, go to Xcode > Preferences... > Downloads tab, then click the 'Download' button for 'Command Line Tools'. You'll need to sign in with an Apple Developer ID.
Using Xcode 4.6 with Command Line Utilities installed
export PATH=/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH
as will execute along with the other command line utilities.