Hi, any other emacs users here?
As an emacs-user, being forced to work in an IDE like Visual Studio or Xcode can be quite… demotivating. I mean, the IDEs themselves are not bad, but text-editing, switching to other files and so on is just sooooo painfully slow, mouse-based and confusing.
(If you are a programmer and don’t know emacs, you should take a look: http://www.gnu.org/software/emacs/)
So, what we need is emacs to be capable of working with the UE4 code-base, ideally with intelli-sense and auto-completion. This turned out by far more complicated than I had hoped for.
First off, this is not supposed to be a pure instruction thread, but a discussion on how to improve working with emacs and UE4. I can only provide my current state of a kind-of (some problems persist, see end of the post) working solution for OSX using rtags. Getting this working on Linux shouldn’t be a big problem. Getting it working on Windows is probably impossible (because rtags doesn’t work on windows and I guess UBT forces you to use VC compiler anyway. No clang, no rtags!) so, if you’re programming on Windows, you might want to consider getting a mac
Using (rtags-find-references-at-point), [C-x r ,] to show all uses of the symbol “NAME_INDEX” that rtags could find.
emacs intellisense options
I’m using rtags, but there are other possible solutions. Maybe you can get them to work, or are already using one of them? If so, please tell us.
When searching for “emacs intellisense” the first hit will probably be CEDET and its Semantic Analyzer. The UE4 code base is too complicated, making heavy use of macros and C++11 stuff, semantic will fail parsing buffers half-through and never be able to find symbols.
Then there is etags (for semantic), I can’t remember what exactly went wrong with that, but I couldn’t get it to work.
I did not try ctags, because etags is supposed to be better.
I also did not try gnu-global, because we are not using gcc and due to how the GNU-guys tick, it probably won’t work with clang.
I tried to compile rtags on windows, but it won’t work. And then Windows complained about my PATH envvar beeing too long… oh, Windows, you are so cool, really.
rtags is made for clang and uses clangs features to analyze the code while actually compiling, so if clang is able to understand and compile the codebase, it should be able to output the correct information for Intellisense to work (I guess that is the way xcode gets its Intellisense under the hood, but I’m not sure, it might use ctags though it doesn’t create TAGS files in the source directories).
-a note on xcrun -
I compiled rtags without much of a problem and got it working with a simple project (the rtags codebase). The problem now was, that UBT kinda explicitly calls the clang that is included with xcode by calling xcrun.
I tried to use a non-xcode clang but it won’t work. It simply never finished compiling one of the first Core cpp files. So there seems to be something about the xcode clang that is necessary for UE4 compilation, at least in the context of xcrun. I didn’t try to edit the Mac-Toolchain UBT file to not call xcrun anymore, maybe that would work too, but you would have to rebuilt UBT then.
-a note on memory -
rtags will create an in-memory database of tags. So it will use even more memory when compiling your project. If your machine barely keeps up compiling UE4 the normal way, you will probably run into out-of-memory problems here.
get and install rtags
go to https://github.com/Andersbakken/rtags
install rtags as explained on the website. You will need to install a llvm/clang as told, using the provided homebrew formula or macports.
piping the xcrun call to clang through the rtags wrapper
UBT will call xcrun to call the clang++ that comes with xcode. To intercept the build commands, we need to put a symlink to the wrapper there. First get the path to the current xcode clang++ by using
xcrun -f clang++
the result should be something like this
now this is only a symlink to the file called clang in the same directory but we don’t care, you could delete the file, or rename it, using
sudo mv /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/__clang++
now to create the symlink to the wrapper file
sudo ln -s ~/rtags/bin/gcc-rtags-wrapper.sh /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
now everytime UBT invokes the xcode clang++ to compile something, it will be run through the rtags wrapper file instead.
using rtags rdm
now start the rdm in a new shell by calling (i installed rtags to ~/rtags, so if you installed it somewhere else, change the path accordingly)
cd ~/rtags/bin ./rdm
Now, when compiling the Editor for example, the rdm shell should be doing stuff (namely indexing/tagging files)
To build UE, run in a different shell:
cd path/to/UnrealEngine source Engine/Build/BatchFiles/Mac/SetupMono.sh Engine/Build/BatchFiles/Mac mono Engine/Binaries/DotNET/UnrealBuildTool.exe UE4Editor Mac Debug -deploy
You might want to remove anything from previous builds first using the -clean flag. Then -deploy will do a full rebuild of the engine.
mono Engine/Binaries/DotNET/UnrealBuildTool.exe UE4Editor Mac Debug -clean
please note that since we replaced the official xcode clang++, even a compile through the xcode interface should start indexing the files for rtags.
using rtags in emacs
This is also stated on the rtags website, but if you’re using your emacs also on windows, you might want to narrow the initialization to the mac only, put this to your emacs initialization scripts:
(when (eq system-type 'darwin) ; only if on mac (require 'cl-lib) (add-to-list 'load-path "~/rtags/src") (require 'rtags) (rtags-enable-standard-keybindings c-mode-base-map) ;;(require 'rtags-ac) ;;(add-to-list 'ac-sources 'ac-source-rtags) ;;(add-to-list 'ac-sources 'ac-source-yasnippet)) (require 'company-rtags))
note: you can use the company-rtags or the rtags-ac integration, depending on which mode you use when editing c++ files.
I personally found ac being much to slow because it tends to provide lists of text of “anything” you have lying around, which is quite useful in python, but like hell in c++. The problem with both modes is nonetheless, that they don’t provide the logical completion-lists in UE4 most of the time. I’m not sure what is the problem, either rtags, company or something on my personal setup, but I currently find company as well as ac pretty useless in c++.
Also, rtags won’t always be able to find symbols. This generally happens when a file you are in, or where the symbol is used in, is not indexed by rtags. Not sure why this happens, because technically everything that runs through the clang compiler should be indexed, so I blame UBT
To help with those cases, I make heavy use of another tool called ag (the silver searcher, https://github.com/ggreer/the_silver_searcher), it is a much better alternative to grep. So whenever a file is not indexed or so, searching with ag (ag-project) will find every location of a certain string and list them in a new buffer with a ‘click to show in other window’ fashion to quickly browse through the results.
So when you know you are looking for the implementation of DoSomething in a class called SomethingClass, and rtags can’t find it, you can tell ag to search for the string “SomethingClass::DoSomething” and you will most likely find the file you are looking for within the results listed by ag.
So, I hope I could get any potential emacs users started to use emacs for UE4 and I even more so hope that I am not the only one using emacs around here and that we can share whatever information and experience we come across while trying to improve that setup.