Register xdg-open for xdot in WSL

I have been using LLVM control-flow graph (CFG) pass for my static analysis research. Normally, I should be able to see the CFG of the intermediate representation of the program (IR) by running:

opt file.ll -passes=view-cfg

Instead of opening xdot that will view the CFG, opt opened Google Chrome and downloaded the file. Previously I just work around this by using the dot-cfg pass and then run xdot manually.

opt file.ll -passes=dot-cfg

However, I am getting curious this morning because I want to reduce the additional step of generating dot files and running xdot. Apparently, the issue must be around mime typing. This is also might be caused by I am running the view-cfg pass inside WSL which doesn't have a desktop environment, instead of full Linux distribution, hence the mime typing is not handled automatically. I have configured the X server so the issue is not because xdot won't open. In fact, chrome can open easily and I also have been using different Jebtrains IDE from my WSL. There is plenty of guide in the internet for anyone who want to run desktop from WSL.

The terminal programs, such as opt, will open desktop applications using xdg-open. xdg-open decides which program to open using the mime type. First, I want to see what is the mime type of the dot file is identified by xdg-open.

$ xdg-mime query filetype .main.dot
application/text

It is clear now the reason why xdg-open decided to open Chrome. It might be a fallback for any application/text file.

Next, I will need to register xdot as a desktop application that will handle *.dot file. We can do it by adding desktop in a new file under ~/.local/share/applications/xdot.desktop. I am getting this guide from this blog.

[Desktop Entry]                                                               
Name=Xdot                                                                     
Exec=/usr/bin/xdot                                                            
MimeType=text/vnd.graphviz                                                     
Type=Application

However, we still need one more additional step. Which is to register that *.dot file will be having text/vnd.graphviz mime. Following the guide here, we need to install libfile-mimeinfo-perl. Then add the following file in /usr/share/mime/packages/xdot.xml.

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="text/vnd.graphviz">
    <glob weight="100" pattern="*.dot"/>
  </mime-type>
</mime-info>

Then, we run update-mime-database /usr/share/mime and check again the mime type.

$ xdg-mime query filetype .main.dot
text/vnd.graphviz

It is working now. Last, we will run view-cfg pass again.

opt file.ll -passes=view-cfg

There is "800 permission denied error" that appeared in the terminal. But I decided to ignore it because at least the xdot can open the dot file now.