Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions .claude/agents/ida-cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,111 @@ ida_add_plugin(myanalyzer
ida-plugin.json
)
```

## Precompiled Headers (PCH)

Precompiled headers significantly speed up compilation by pre-parsing stable headers that rarely change (IDA SDK, STL).

### When to Use PCH

- Projects with multiple source files
- Development cycles with frequent rebuilds
- Large plugins that include many SDK headers

### PCH Template

Use the `plugin-pch` template for projects with PCH support:

```bash
cp -r $IDASDK/ida-cmake/templates/plugin-pch/* my-plugin/
```

### PCH File Structure

Create a `pch.h` file with stable headers:

```cpp
// pch.h - Precompiled header
#pragma once

#define PLUGIN_PCH_INCLUDED // Marker for fallback includes

// Standard Library
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>

// IDA SDK Core
#include <pro.h>
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <bytes.hpp>
#include <funcs.hpp>
#include <auto.hpp>
#include <nalt.hpp>
#include <netnode.hpp>
#include <segment.hpp>
#include <name.hpp>
#include <ua.hpp>
#include <xref.hpp>

// Hex-Rays (if needed)
// #include <hexrays.hpp>
```

### CMake PCH Configuration

Add PCH support to CMakeLists.txt:

```cmake
ida_add_plugin(myplugin
SOURCES
main.cpp
plugin.h
pch.h
DEBUG_ARGS
"${SAMPLE_IDB}"
)

# Enable PCH
option(USE_PCH "Use precompiled headers" ON)

if(USE_PCH)
target_precompile_headers(myplugin PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/pch.h>"
)
endif()
```

### Fallback Headers in Plugin Header

Your `plugin.h` should handle both PCH and non-PCH builds:

```cpp
// plugin.h
#pragma once

// Fallback when PCH is disabled
#ifndef PLUGIN_PCH_INCLUDED
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#endif

// Plugin-specific declarations
#define PLUGIN_NAME "MyPlugin"
```

### Disable PCH

To build without PCH:

```bash
cmake -B build -DUSE_PCH=OFF
```
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Most users don't need these targets unless developing custom debugger plugins.
Ready-to-use templates are available in `$IDASDK/ida-cmake/templates/`:

- `plugin/` - Basic plugin template (convenience functions)
- `plugin-pch/` - Plugin with precompiled header support (faster builds)
- `plugin-vanilla/` - Plugin using standard CMake commands
- `plugin-no-bootstrap/` - Plugin using CMAKE_PREFIX_PATH approach (no bootstrap include)
- `loader/` - File loader template
Expand Down
49 changes: 49 additions & 0 deletions templates/plugin-pch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.27)
project(myplugin)
set(CMAKE_CXX_STANDARD 17)

# Include IDA SDK bootstrap
include($ENV{IDASDK}/ida-cmake/bootstrap.cmake)
find_package(idasdk REQUIRED)

# Convert paths to native format
set(SAMPLE_IDB "${IDA_CMAKE_DIR}/samples/wizmo32.exe.i64")
file(TO_NATIVE_PATH "${SAMPLE_IDB}" SAMPLE_IDB)

# Add plugin
ida_add_plugin(myplugin
SOURCES
main.cpp
plugin.h
pch.h
DEBUG_ARGS
"${SAMPLE_IDB}"
)

# ============================================================================
# Precompiled Header Support
# ============================================================================
# PCH significantly speeds up compilation by pre-parsing stable headers.
# Disable with: cmake -B build -DUSE_PCH=OFF
# ============================================================================

option(USE_PCH "Use precompiled headers for faster builds" ON)

if(USE_PCH)
target_precompile_headers(myplugin PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/pch.h>"
)
endif()

# IDA 9.x deployment with metadata (uncomment to use)
# Plugin deploys to: $IDABIN/plugins/myplugin/ with ida-plugin.json
# ida_add_plugin(myplugin
# SOURCES
# main.cpp
# plugin.h
# pch.h
# DEBUG_ARGS
# "${SAMPLE_IDB}"
# METADATA_JSON
# ida-plugin.json
# )
39 changes: 39 additions & 0 deletions templates/plugin-pch/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file main.cpp
* @brief IDA plugin implementation with PCH support.
*/

#include "plugin.h"

// Plugin module class
struct sample_plugin_t : public plugmod_t
{
virtual bool idaapi run(size_t) override
{
msg("Hello from %s!\n", PLUGIN_NAME);
info("This plugin was built with precompiled header support.");
return true;
}
};

// Plugin initialization
static plugmod_t* idaapi init()
{
msg("%s: initialized\n", PLUGIN_NAME);
return new sample_plugin_t;
}

// Plugin description
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_MULTI,
init,
nullptr,
nullptr,
"Sample Plugin with PCH - Demonstrates precompiled header usage.",
"Sample Plugin with PCH\n"
"This plugin template uses precompiled headers for faster builds.",
PLUGIN_NAME,
PLUGIN_HOTKEY
};
37 changes: 37 additions & 0 deletions templates/plugin-pch/pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file pch.h
* @brief Precompiled header for IDA plugin.
*
* This header includes stable IDA SDK headers that rarely change.
* Using PCH significantly speeds up compilation by pre-parsing these headers.
*
* To disable PCH, configure with: cmake -B build -DUSE_PCH=OFF
*/
#pragma once

// Marker to indicate PCH is being used
#define PLUGIN_PCH_INCLUDED

// Standard Library Headers
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>

// IDA SDK Core Headers
#include <pro.h>
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <bytes.hpp>
#include <funcs.hpp>
#include <auto.hpp>
#include <nalt.hpp>
#include <netnode.hpp>
#include <segment.hpp>
#include <name.hpp>
#include <ua.hpp>
#include <xref.hpp>
26 changes: 26 additions & 0 deletions templates/plugin-pch/plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file plugin.h
* @brief Plugin header - declarations and constants.
*
* Note: Most includes come from pch.h (precompiled header).
* This file only contains plugin-specific declarations.
*/
#pragma once

// ============================================================================
// Fallback includes when PCH is disabled
// ============================================================================

#ifndef PLUGIN_PCH_INCLUDED
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#endif

// ============================================================================
// Plugin Constants
// ============================================================================

#define PLUGIN_NAME "MyPlugin"
#define PLUGIN_HOTKEY "Ctrl-Shift-P"